Welcome back! 👋
Now that we have our database and tables set up with proper data types and constraints, it’s time to learn the most important SQL commands — how to read and view data.
By the end of this lesson, you’ll be able to:
- Retrieve data from a table using
SELECT - Filter results using
WHERE - Sort results using
ORDER BY - Use basic aggregate functions (
COUNT,SUM,AVG) - Practice queries with our sample database
Retrieving Data with SELECT
The SELECT statement is the most used SQL command. It lets you choose columns to view from a table.
Example: Get all customers
SELECT * FROM customers; Copy
*means “all columns”- This query returns every row and every column in the
customerstable
Example: Get specific columns
SELECT full_name, email, city FROM customers; Copy
- Only shows full_name, email, and city
- Helps make results cleaner
Example: Rename columns in results using AS
SELECT full_name AS 'Customer Name', city AS 'City' FROM customers; Copy
- Makes output more readable, especially in reports
Filtering Data with WHERE
The WHERE clause lets you filter rows based on a condition.
Example: Get customers from Pakistan
SELECT full_name, city, country FROM customers WHERE country = 'Pakistan'; Copy
Example: Get customers who signed up after Feb 1, 2023
SELECT full_name, signup_date FROM customers WHERE signup_date > '2023-02-01'; Copy
Logical Operators: AND, OR, NOT
-- Customers from Pakistan AND city is Karachi SELECT full_name, city FROM customers WHERE country = 'Pakistan' AND city = 'Karachi'; -- Customers from Pakistan OR USA SELECT full_name, country FROM customers WHERE country = 'Pakistan' OR country = 'USA'; -- Customers NOT from Pakistan SELECT full_name, country FROM customers WHERE NOT country = 'Pakistan'; Copy
Comparison Operators
| Operator | Example | Meaning |
|---|---|---|
= | country = 'USA' | Equal to |
!= | country != 'USA' | Not equal to |
> | price > 100 | Greater than |
< | price < 100 | Less than |
>= | quantity >= 1 | Greater or equal |
<= | quantity <= 5 | Less or equal |
BETWEEN | signup_date BETWEEN '2023-01-01' AND '2023-03-01' | Between two values |
LIKE | full_name LIKE 'A%' | Starts with ‘A’ |
Sorting Results with ORDER BY
You can order the results using ORDER BY:-- Sort customers by signup date ascending SELECT full_name, signup_date FROM customers ORDER BY signup_date ASC; -- Sort customers by signup date descending SELECT full_name, signup_date FROM customers ORDER BY signup_date DESC; Copy
Aggregate Functions
Aggregate functions help summarize data:
| Function | What it Does | Example |
|---|---|---|
COUNT() | Counts rows | Count total customers |
SUM() | Sum of values | Total sales amount |
AVG() | Average | Average price of products |
MIN() | Minimum value | Cheapest product price |
MAX() | Maximum value | Most expensive product price |
Examples:
-- Count total customers SELECT COUNT(*) AS total_customers FROM customers; -- Sum of all order amounts SELECT SUM(total_amount) AS total_sales FROM orders; -- Average product price SELECT AVG(price) AS avg_price FROM products; -- Most expensive product SELECT MAX(price) AS highest_price FROM products; Copy
Filtering Aggregates with HAVING
If you combine aggregates, use HAVING to filter results:-- Total order amount per customer SELECT customer_id, SUM(total_amount) AS total_spent FROM orders GROUP BY customer_id HAVING SUM(total_amount) > 1000; Copy
GROUP BYgroups rows by a columnHAVINGfilters after aggregation (similar to WHERE, but for grouped data)
Practice Queries
- Show all products under $100
- Show all orders for customer_id = 1
- Count how many orders each customer has placed
- Show the total amount each customer has spent, sorted descending
Try writing these queries using our sample database — practice is the fastest way to learn SQL.
Lesson Summary
SELECTlets you choose which columns to seeWHEREfilters rows based on conditionsORDER BYsorts the results- Aggregate functions summarize data:
COUNT,SUM,AVG,MIN,MAX GROUP BY+HAVINGallow grouping and filtering aggregates- Practice writing queries on
customers,products,orders, andorder_items
In Lesson 4, we’ll dive into advanced filtering and conditional logic:
- Combining multiple conditions
- Using
IN,BETWEEN,LIKE - Handling
NULLvalues - More complex examples for real-world scenarios