0% found this document useful (0 votes)
32 views

Part 2 - Sorting and Filtering

This document discusses sorting and filtering in SQL. It covers: - Using the ORDER BY clause to sort data retrieved in a SELECT statement by one or more columns in ascending or descending order. - Using the WHERE clause to filter data retrieved in a SELECT statement by specifying criteria for one or more columns using operators like =, <>, IN, LIKE, etc. - Combining AND, OR, and parentheses in the WHERE clause to filter by multiple criteria. - Using wildcards like % in the LIKE operator to filter string columns by partial matches. - The NOT operator to return records that do not match the criteria. - Examples of sorting, filtering, and combining techniques on tables like

Uploaded by

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

Part 2 - Sorting and Filtering

This document discusses sorting and filtering in SQL. It covers: - Using the ORDER BY clause to sort data retrieved in a SELECT statement by one or more columns in ascending or descending order. - Using the WHERE clause to filter data retrieved in a SELECT statement by specifying criteria for one or more columns using operators like =, <>, IN, LIKE, etc. - Combining AND, OR, and parentheses in the WHERE clause to filter by multiple criteria. - Using wildcards like % in the LIKE operator to filter string columns by partial matches. - The NOT operator to return records that do not match the criteria. - Examples of sorting, filtering, and combining techniques on tables like

Uploaded by

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

Database Concepts and Skills for Big

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;

This statement returns the list of customer last names, ordered


alphabetically
Sorting
❑ The ORDER BY clause comes after FROM clause:

(1) SELECT Col_name


(2) FROM table_name
(3) ORDER BY Col_name;
Sorting by multiple columns
❑ The ORDER BY statement can sor t data by more than one column:

SELECT Col1_na m e , Col2_na me , Col3_na me , …


FROM table_na m e
ORDER BY Col1_na m e , Col2_na m e;

<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

❑ To sort by descending order, we should use the DESC keyword.

<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:

SELECT Col1_name, Col2_name, Col3_name, …


FROM table_name
WHERE criteria_clause;
Filtering – Numerical values
<Example>
SELECT ItemID, ItemDescription, ItemPrice
FROM ITEM
WHERE ItemPrice = 250;

• 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’;

• This statement will return all employees


whose last name is Stuart.
• Note that the last name is enclosed in single
quotation.
• When searching for textual value, it should be
enclosed in double quotation or single
quotation.
Where Clause Operators
• The following conditional operators can be used in a WHERE
clause:
Filtering
<Example>
SELECT EmployeeID, LastName,
FirstName
FROM EMPLOYEE
WHERE LastName <> ‘Stuart’;

• This statement will return all


employees except those whose last
name is Stuart.
Filtering – Multiple Criteria
❑ To filter by more than one criteria, we can use AND clause
and OR clause.
❑ AND clause returns the records that satisfy ALL criteria.
❑ OR clause returns the records that satisfy ANY of criteria.
Filtering – Multiple Criteria - AND
Example: find employee with last name ‘Stuart’ and first name
‘Anne’:
SELECT *
FROM EMPLOYEE
WHERE LastName = ‘Stuart’ AND FirstName = ‘Anne’;

There is only one instance that satisfies both conditions.


Filtering – Multiple Criteria - OR
• Example: find customers with last name ‘Shire’ or ‘Anderson’
SELECT *
FROM CUSTOMER
WHERE LastName = ‘Shire’ OR LastName = ‘Anderson’;

• 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:

<Example > SELECT *

SELECT * FROM VENDOR


same as
F RO M V E N D O R WHERE City = ‘Seattle’
W H E R E C i t y I N ( ‘ S e at tl e ’ , ‘ S a n Fr a nc i s c o ’ ) ; OR City = ‘San Francisco’;

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!

You might also like