SELECT
WHERE
ORDER BY
GROUP BY
HAVING
Activity Solutions
The SELECT statement is used to pick the columns you want to see.
Basic Syntax:
The Wildcard (Selects all columns):
Sometimes a column contains many duplicate values. To see only unique values, use DISTINCT.
Syntax:
Example:
Use the Chinook database to answer the following questions:
FirstName, LastName, and Email of every customer.Customer table only once.Name, Composer, and UnitPrice of every track.SELECT
WHERE
ORDER BY
GROUP BY
HAVING
Activity Solutions
The WHERE clause is used to filter records based on specific conditions.
Example:
Use these to compare values in your WHERE clause:
= (Equal to)!= (Not equal to)> (Greater than)< (Less than)>= (Greater than or equal to)<= (Less than or equal to)You can combine multiple conditions using logic:
Example:
Use the Chinook database to answer the following questions:
Name and UnitPrice.SELECT
WHERE
ORDER BY
GROUP BY
HAVING
Activity Solutions
The ORDER BY clause is used to sort the result set.
Example:
You can sort by more than one column.
Example:
Use the Chinook database to answer the following questions:
Country, then by LastName, and then by FirstName, all in ascending order.UnitPrice. For tracks with the same price, sort by Name in ascending order.Total.SELECT
WHERE
ORDER BY
GROUP BY
HAVING
Activity Solutions
Aggregation is the process of performing a calculation on a set of values to return a single value.
When using aggregate functions, we often want to see the summary per category.
GROUP BY tells the database how to bucket the data.Example:
Use the Chinook database to answer the following questions:
GenreId.BillingCountry, calculate the number of invoices and the total amount invoiced.AlbumId, calculate the average track length in milliseconds.SELECT
WHERE
ORDER BY
GROUP BY
HAVING
Activity Solutions
WHERE cannot be used with aggregate functions. For that, we use HAVING.
Example:
| Feature | WHERE | HAVING |
|---|---|---|
| Usage | Filters Rows | Filters Groups |
| Timing | Before Grouping | After Grouping |
| Aggregates | Cannot use SUM(), AVG() |
Used with SUM(), AVG() |
SQL is not read from top to bottom by the database engine. It follows a specific internal order:
Use the Chinook database to answer the following questions:
GenreId to identify each genre.SELECT
WHERE
ORDER BY
GROUP BY
HAVING
Activity Solutions
-- 1. Tracks that cost at least $1.00
SELECT Name, UnitPrice
FROM Track
WHERE UnitPrice >= 1.00;
-- 2. Customers in the USA or Canada
SELECT FirstName, LastName, Country
FROM Customer
WHERE Country = 'USA' OR Country = 'Canada';
-- 3. USA invoices greater than $10.00
SELECT InvoiceId, BillingCountry, Total
FROM Invoice
WHERE BillingCountry = 'USA' AND Total > 10.00;-- 1. Customers sorted by country and name
SELECT FirstName, LastName, Country
FROM Customer
ORDER BY Country ASC, LastName ASC, FirstName ASC;
-- 2. Tracks sorted by price and then name
SELECT Name, UnitPrice
FROM Track
ORDER BY UnitPrice DESC, Name ASC;
-- 3. Invoices sorted from highest to lowest total
SELECT InvoiceId, CustomerId, Total
FROM Invoice
ORDER BY Total DESC;-- 1. Number of tracks in each genre
SELECT GenreId, COUNT(*) AS TrackCount
FROM Track
GROUP BY GenreId;
-- 2. Invoice count and total amount by country
SELECT BillingCountry,
COUNT(*) AS InvoiceCount,
SUM(Total) AS TotalInvoiced
FROM Invoice
GROUP BY BillingCountry;
-- 3. Average track length for each album
SELECT AlbumId, AVG(Milliseconds) AS AverageMilliseconds
FROM Track
GROUP BY AlbumId;-- 1. Countries with more than $100 in invoice totals
SELECT BillingCountry, SUM(Total) AS TotalInvoiced
FROM Invoice
GROUP BY BillingCountry
HAVING SUM(Total) > 100;
-- 2. Genres with more than 100 tracks
SELECT GenreId, COUNT(*) AS TrackCount
FROM Track
GROUP BY GenreId
HAVING COUNT(*) > 100;
-- 3. Countries with an average invoice total over $6.00
SELECT BillingCountry, AVG(Total) AS AverageInvoiceTotal
FROM Invoice
GROUP BY BillingCountry
HAVING AVG(Total) > 6.00;