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:

  1. Retrieve data from a table using SELECT
  2. Filter results using WHERE
  3. Sort results using ORDER BY
  4. Use basic aggregate functions (COUNTSUMAVG)
  5. 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 customers table

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

OperatorExampleMeaning
=country = 'USA'Equal to
!=country != 'USA'Not equal to
>price > 100Greater than
<price < 100Less than
>=quantity >= 1Greater or equal
<=quantity <= 5Less or equal
BETWEENsignup_date BETWEEN '2023-01-01' AND '2023-03-01'Between two values
LIKEfull_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:

FunctionWhat it DoesExample
COUNT()Counts rowsCount total customers
SUM()Sum of valuesTotal sales amount
AVG()AverageAverage price of products
MIN()Minimum valueCheapest product price
MAX()Maximum valueMost 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 BY groups rows by a column
  • HAVING filters after aggregation (similar to WHERE, but for grouped data)

Practice Queries

  1. Show all products under $100
  2. Show all orders for customer_id = 1
  3. Count how many orders each customer has placed
  4. 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

  • SELECT lets you choose which columns to see
  • WHERE filters rows based on conditions
  • ORDER BY sorts the results
  • Aggregate functions summarize data: COUNTSUMAVGMINMAX
  • GROUP BY + HAVING allow grouping and filtering aggregates
  • Practice writing queries on customersproductsorders, and order_items

In Lesson 4, we’ll dive into advanced filtering and conditional logic:

  • Combining multiple conditions
  • Using INBETWEENLIKE
  • Handling NULL values
  • More complex examples for real-world scenarios

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts