SELECT statement, and learning to write precise, efficient queries is one of the most valuable skills you can build. This chapter covers the full SELECT toolkit: choosing columns, filtering rows with WHERE, eliminating duplicates with DISTINCT, sorting with ORDER BY, paginating with LIMIT and OFFSET, performing calculations with aggregate functions, grouping data with GROUP BY, and filtering groups with HAVING. Throughout, you’ll use the sample Employee and Department tables you set up in the previous chapter.
Reference Tables
All examples in this chapter use these tables: Department:
Employee:
SELECT Statement
TheSELECT statement retrieves data from one or more tables.
Retrieve all columns:
Practice: Display names and cities
Practice: Display names and cities
Write a query to display only employee names and cities.
Column Aliases
UseAS to give a column a temporary display name. Aliases make output easier to read and are especially useful when performing calculations.
Practice: Rename columns
Practice: Rename columns
Display employee names as Employee and salaries as Salary.
DISTINCT
DISTINCT removes duplicate values from a result set, returning only unique values.
Practice: Unique department IDs
Practice: Unique department IDs
WHERE Clause
TheWHERE clause filters rows based on a condition. Only rows that satisfy the condition appear in the result.
Comparison Operators
Practice: Salaries under ₹60,000
Practice: Salaries under ₹60,000
Logical Operators
Combine multiple conditions withAND, OR, and NOT.
AND — both conditions must be true:
Practice: Hyderabad employees earning more than ₹65,000
Practice: Hyderabad employees earning more than ₹65,000
BETWEEN
BETWEEN tests whether a value falls within an inclusive range.
Practice: Salaries between ₹55,000 and ₹80,000
Practice: Salaries between ₹55,000 and ₹80,000
IN
IN checks whether a value matches any item in a list, replacing multiple OR conditions.
Practice: Employees in departments 2 and 3
Practice: Employees in departments 2 and 3
LIKE
LIKE matches text patterns using wildcard characters.
Practice: Names starting with S
Practice: Names starting with S
NULL Values
UseIS NULL or IS NOT NULL to find rows where a value is missing or present.
ORDER BY
ORDER BY sorts the result set by one or more columns.
Practice: Sort by employee name
Practice: Sort by employee name
LIMIT and OFFSET
LIMIT restricts how many rows are returned. OFFSET skips a number of rows before starting to return results — useful for pagination.
Practice: First four employees
Practice: First four employees
Aggregate Functions
Aggregate functions perform calculations across multiple rows and return a single value.Practice: Find minimum salary
Practice: Find minimum salary
GROUP BY
GROUP BY groups rows that share the same value in one or more columns, then applies an aggregate function to each group.
Practice: Count employees per city
Practice: Count employees per city
HAVING
HAVING filters groups after aggregation, whereas WHERE filters individual rows before grouping. Use HAVING whenever your filter condition involves an aggregate function.
Practice: Cities with more than one employee
Practice: Cities with more than one employee
SQL Execution Order
You write SQL clauses in this order:SELECT cannot be used in WHERE — WHERE runs before SELECT, so the alias doesn’t exist yet at that point.
Summary
In this chapter you learned how to:- Retrieve data using
SELECTwith specific columns or* - Rename output columns with
ASaliases - Remove duplicates using
DISTINCT - Filter rows with
WHERE, comparison operators,AND/OR/NOT,BETWEEN,IN,LIKE, andIS NULL - Sort results with
ORDER BY(ascending and descending) - Limit rows with
LIMITandOFFSET - Calculate totals, averages, counts, and extremes with aggregate functions
- Group rows with
GROUP BY - Filter groups with
HAVING - Understand SQL’s execution order