Lesson 5 of 20

SELECT - Querying Data

Basic SELECT & WHERE Clause

The SELECT statement is the most commonly used SQL command. It retrieves data from one or more tables. You can select all columns with * or specify individual columns by name.

The WHERE clause filters rows based on conditions, allowing you to retrieve only the data that matches your criteria.

  • SELECT *: Retrieves all columns from the table
  • SELECT column1, column2: Retrieves only specified columns
  • WHERE: Filters results based on a condition
  • AS: Creates an alias (temporary name) for a column in the result
Example
-- Select all columns and all rows
SELECT * FROM users;

-- Select specific columns
SELECT username, email, age FROM users;

-- Use WHERE to filter results
SELECT username, age FROM users
WHERE age > 25;

-- Use column aliases
SELECT username AS name, email AS contact
FROM users;
Notes
  • Avoid using SELECT * in production applications. Always specify the columns you need — it improves performance and makes your code more readable.

Comparison Operators & Logical Operators

SQL provides comparison operators to build conditions in your WHERE clause. You can combine multiple conditions using logical operators AND, OR, and NOT.

These operators let you create powerful filters to find exactly the data you need.

  • = : Equal to
  • != or <>: Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
  • AND: Both conditions must be true
  • OR: At least one condition must be true
  • NOT: Reverses the condition
Example
-- Find users who are exactly 28 years old
SELECT * FROM users WHERE age = 28;

-- Find active users over 25
SELECT * FROM users
WHERE age > 25 AND is_active = TRUE;

-- Find users who are 22 or 34
SELECT * FROM users
WHERE age = 22 OR age = 34;

-- Find users who are NOT active
SELECT * FROM users
WHERE NOT is_active;
Notes
  • Use parentheses to clarify complex conditions: WHERE (age > 25 AND is_active = TRUE) OR username = 'admin'.

LIKE, IN, and BETWEEN

SQL provides special operators for pattern matching and range checks. LIKE lets you search for patterns in text, IN checks if a value matches any value in a list, and BETWEEN checks if a value falls within a range.

The LIKE operator uses two wildcard characters: % matches zero or more characters, and _ matches exactly one character.

Example
-- LIKE: Find usernames starting with 'a'
SELECT * FROM users WHERE username LIKE 'a%';

-- LIKE: Find emails containing 'example'
SELECT * FROM users WHERE email LIKE '%example%';

-- LIKE: Find usernames with exactly 3 characters
SELECT * FROM users WHERE username LIKE '___';

-- IN: Find users with specific ages
SELECT * FROM users WHERE age IN (22, 28, 34);

-- BETWEEN: Find users aged 25 to 35 (inclusive)
SELECT * FROM users WHERE age BETWEEN 25 AND 35;

-- BETWEEN with dates
SELECT * FROM users
WHERE join_date BETWEEN '2026-01-01' AND '2026-06-30';
Notes
  • LIKE is case-insensitive in MySQL by default but case-sensitive in PostgreSQL. Use ILIKE in PostgreSQL for case-insensitive pattern matching.