Part 2 - Sorting and Filtering
Part 2 - Sorting and Filtering
Data
Le Duy Dung (Andrew)
SQL – Part 2
Sorting and Filtering
Sorting
❑ The ORDER BY statement sorts data retrieved by select statements:
SELECT Col_name
FROM table_name
ORDER BY Col_name;
<Example>
SELECT LastName
FROM CUSTOMER
ORDER BY LastName;
<Example>
SELECT LastName , FirstName , CustomerID
FROM CUSTOMER
ORDER BY LastName , FirstName ;
This statement returns the list of customers, sorted by last name and first
name.
The customers are ordered by their last name; if there is more then one
customer with the same last name, those customers are sorted by their last
name and first name.
Sorting direction
❑ By default, the ORDER BY clause sort the records as follows:
• Text: Alphabetically (from A to Z)
• Numbers: Ascending
<Example>
SELECT LastName
FROM CUSTOMER
ORDER BY LastName DESC;
Sorting direction
❑ To sort data by mixed direction we have:
<Example>
SELECT LastName , FirstName CustomerID
FROM CUSTOMER
ORDER BY LastName , FirstName DESC;
The DESC
keyword only
applied to the
FirstName column
Filtering
❑ In most cases, we are only interested in a subset of the data in a
table.
❑ To extract a subset of table, we need to specify a criteria
❑ The WHERE clause in SQL is used to define a search criteria as
follows:
• This statement will return all items with $250 price tag.
• There are only 4 of 25 items have $250 price tag.
• The returned records are the subset of the ITEM table.
Filtering
<Example>
SELECT EmployeeID, LastName, FirstName
FROM EMPLOYEE
WHERE LastName = ‘Stuart’;
• Example: find items from vendor with ID 1 or items that cost more than
$3000
SELECT *
FROM ITEM
WHERE VendorID = 1 OR ItemCost >= 3000;
Filtering – Multiple Criteria - OR
• Example: Find items from vendor with ID 1 or 2 that the price is
higher than $1000.
Filtering – Multiple Criteria - OR
• Example: find items from vendor with ID 1 or 2 that the price is
higher than $1000.
SELECT *
FROM ITEM
WHERE (VendorID = 1 OR VendorID = 2) AND ItemPrice >=1000;
• The following would not work. The problem is that order of evaluation. SQL
processes AND operator before OR operator.
SELECT *
FROM ITEM
WHERE VendorID = 1 OR VendorID = 2 AND ItemPrice >=1000;
Filtering – Multiple Criteria - IN
❑ The IN operator functions similar to OR operator
❑ We can define a list of conditions as follows:
This returns the list of all vendors in Seattle and San Francisco
Wildcard Filtering
❑To filter a field by a part of a text (string), we can use wildcards
❑The wildcard character is a percent sign % (* in Access)
❑We can use the wildcards anywhere in the text
❑When using wildcard for filtering, instead of equal sign, use LIKE keyword
<Example >
S E L E C T C o l _ n ame
F RO M t a bl e _ nam e
W H E R E c o l _ n am e L I K E ‘ X X X X % ’ ;
Wildcard Filtering
Example: Find all the items whose descriptions started with the word “Antique”
SELECT *
F RO M I T E M
W H E R E I t e m D e s c ri pt i o n L I K E ‘ A n ti que * ’ ;
Wildcard Filtering
Example: Find all the items whose descriptions ended with “Desk”
SELECT *
F RO M I T E M
W H E R E I t e m D e s c ri pt i o n L I K E ‘ * D e s k ’ ;
Wildcard Filtering
Example: Find all the customers whose addresses contain “NE”
SELECT *
F RO M C U S TO M ER
W H E R E A dd re s s L I K E ‘ * N E * ’ ;
NOT Operator
❑NOT will return the opposite of the statement
<Example>
SELECT *
FROM CUSTOMER
WHERE NOT City = ‘Seattle’;
This statement returns all customers who are not from Seattle
Practice Questions
1. Sort CUSTOMERS by their first names (just return customers first name
column)
2. Sort CUSTOMERS table by customers first names (return all columns)
3. Sort CUSTOMERS by their first names and just return customers last
name column.
4. Sort CUSTOMERS table by customers’ addresses, city, and ZIP (return all
columns).
5. Sort the items by their price from most expensive to the least expensive
(return all columns).
6. Return the item description of the 5 most expensive items
Practice Questions
7. Find all customers from Bellevue
8. Return all items that are more expensive than $500
9. Return all vendors except for European Specialties
10. Find all the sales before 1/5/2019
(Hint: put # before and after the query date 1/5/2019)
11. Find all the items whose descriptions start with “Dining Table”
12. Find all the vendors with contact last name is Nelson or contact first
name is Andrew
13. We are looking for a customer. We know her address has the word
‘Aloha’. Find the customer.
QnA!