INFOMAN Lesson 10 SQL Select
INFOMAN Lesson 10 SQL Select
https://www.w3schools.com/sql/sql_select.asp
Objectives
If you want to select all the fields available in the table, use the following syntax:
3
Below is a selection from the "Customers" table in the Northwind sample database:
Command:
SELECT CustomerName, City FROM Customers;
5
The SQL SELECT DISTINCT Statement
Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
6
SELECT Example Without DISTINCT
7
SELECT DISTINCT Country FROM Customers;
8
SQL WHERE Clause
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:
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 ...;
14
OR Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3
...;
15
NOT Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
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):
17
The following SQL statement selects all fields from "Customers" where country is
NOT "Germany" and NOT "USA":
18
SQL ORDER BY 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