Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

INFOMAN Lesson 10 SQL Select

This document provides an overview of basic SQL SELECT commands including the SELECT statement, SELECT DISTINCT, WHERE clause, AND, OR and NOT operators, ORDER BY keyword. It includes syntax examples and explanations for selecting data from database tables and filtering/sorting the results.

Uploaded by

ROMMEL DORIN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

INFOMAN Lesson 10 SQL Select

This document provides an overview of basic SQL SELECT commands including the SELECT statement, SELECT DISTINCT, WHERE clause, AND, OR and NOT operators, ORDER BY keyword. It includes syntax examples and explanations for selecting data from database tables and filtering/sorting the results.

Uploaded by

ROMMEL DORIN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Lesson 10

SQL Select and Insert Into


Janice Pola D. Congzon

https://www.w3schools.com/sql/sql_select.asp
Objectives

▪ Learn the basic SQL Select Commands


▫ Select Statement
▫ Select Distinct
▫ Where
▫ AND, OR, NOT
▫ Order By
SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
Syntax:

Here, column1, column2, ... are


SELECT column1, column2, ...
the field names of the table you
FROM table_name;
want to select data from.

If you want to select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

3
Below is a selection from the "Customers" table in the Northwind sample database:

SELECT Column Example Syntax:


SELECT column1, column2, ...FROM table_name;

Command:
SELECT CustomerName, City FROM Customers;

SELECT * FROM Customers; 4


SELECT Column Example
Command:
SELECT CustomerName, City FROM Customers;

SELECT * FROM Customers;

5
The SQL SELECT DISTINCT Statement

The SELECT DISTINCT statement is used to return only distinct (different)


values.
Inside a table, a column often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.

Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;

6
SELECT Example Without DISTINCT

SELECT Country FROM Customers;

Country Germany Venezuela


Germany France Brazil
Mexico UK
Venezuela
Mexico Austria
USA
UK Brazil
Sweden
Ireland
Spain
Germany UK
France
France Sweden Germany
Spain Germany France
France France France
Canada Italy Canada
UK Portugal USA
Argentina Spain
Germany
Mexico Spain
Switzerland
USA
Brazil
Brazil Venezuela
USA
UK Venezuela

7
SELECT DISTINCT Country FROM Customers;

The following SQL statement lists the


number of different (distinct)
customer countries:

SELECT COUNT(DISTINCT Country)


FROM Customers;

8
SQL WHERE Clause

The WHERE clause is used to filter records.


It is used to extract only those records that fulfill a specified condition.

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE,
etc.! 9
10
The following SQL statement selects all the customers from the country "Mexico", in
the "Customers" table:

SELECT * FROM Customers WHERE Country='Mexico';

SELECT * FROM Customers WHERE CustomerID=1;

11
Operators in The WHERE Clause
Operator Description Example
= Equal SELECT * FROM Products WHERE Price = 18;
> Greater than SELECT * FROM Products WHERE Price >
30;
< Less than SELECT * FROM Products WHERE Price <
30;
>= Greater than or equal SELECT * FROM Products
WHERE Price >= 30;
<= Less than or equal SELECT * FROM Products
WHERE Price <= 18;
<> Not equal. Note: In some SELECT * FROM Products
versions of SQL this operator WHERE Price <> 18;
may be written as !=
BETWEEN Between a certain range SELECT * FROM Products
WHERE Price BETWEEN 50 AND 60;
LIKE Search for a pattern SELECT * FROM Customers
WHERE City LIKE 's%';
IN To specify multiple possible SELECT * FROM Customers 12
SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
•The AND operator displays a record if all the conditions separated by AND are TRUE.
•The OR operator displays a record if any of the conditions separated by OR is TRUE.
•The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND conditio
n3 ...;

13
AND Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND conditio
n3 ...;

SELECT * FROM Customers


WHERE Country='Germany' AND City='Berlin';

14
OR Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3
...;

SELECT * FROM Customers


WHERE City='Berlin' OR City='München';

15
NOT Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

SELECT * FROM Customers


WHERE NOT Country='Germany';

16
Combining AND, OR and NOT
You can also combine the AND, OR and NOT operators.
The following SQL statement selects all fields from "Customers" where country is "Germany" AND
city must be "Berlin" OR "München" (use parenthesis to form complex expressions):

SELECT * FROM Customers


WHERE Country='Germany' AND (City='Berlin' OR City='München');

17
The following SQL statement selects all fields from "Customers" where country is
NOT "Germany" and NOT "USA":

SELECT * FROM Customers


WHERE NOT Country='Germany' AND NOT Country='USA';

18
SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending


order.
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.

Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

19
SELECT * FROM Customers ORDER BY Country;

The following SQL statement selects all customers from the "Customers"
table, sorted by the "Country" column: 20
SELECT * FROM Customers ORDER BY Country DESC;

The following SQL statement selects all customers from the "Customers"
table, sorted DESCENDING by the "Country" column: 21
SELECT * FROM Customers ORDER BY Country, CustomerName;

The following SQL statement selects all customers from the "Customers" table,
sorted by the "Country" and the "CustomerName" column. This means that it
orders by Country, but if some rows have the same Country, it orders them by
22
CustomerName.
SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;

The following SQL statement selects all customers from the "Customers" table,
sorted ascending by the "Country" and descending by the "CustomerName" 23
column
End of
Lesson 10
24

You might also like