DQL - Retrieving Data with SELECT
Data Query Language (DQL) is used to retrieve data from a database. The primary command in DQL is SELECT. Throughout this chapter, we’ll use the following tables.Department
Employee
SELECT Statement
TheSELECT statement retrieves data from one or more tables.
Syntax
Example 1
Retrieve all columns.Example 2
Retrieve only employee names.Example 3
Retrieve employee names and salaries.Practice
Display only the employee names and cities.Solution
Solution
Column Aliases
Aliases provide temporary names to columns, making the output easier to read.Syntax
Example
Example
Practice
Display employee names as Employee and salary as Salary.Solution
Solution
DISTINCT
DISTINCT removes duplicate values.
Syntax
Example
Example
Practice
Display all unique department IDs.Solution
Solution
WHERE Clause
TheWHERE clause filters rows based on a condition.
Syntax
Example
Example
Example
Practice
Display employees earning more than ₹70,000.Solution
Solution
Comparison Operators
Example
Practice
Display employees whose salary is less than ₹60,000.Solution
Solution
Logical Operators
Logical operators combine multiple conditions.AND
Both conditions must be true.OR
At least one condition must be true.NOT
Negates a condition.Practice
Display employees from Hyderabad earning more than ₹65,000.Solution
Solution
BETWEEN
BETWEEN checks whether a value falls within a range.
Example
Practice
Display employees with salaries between ₹55,000 and ₹80,000.Solution
Solution
IN
IN checks whether a value exists in a list.
Example
Practice
Display employees belonging to departments 2 and 3.Solution
Solution
LIKE
LIKE searches for text patterns.
Example
Example
Practice
Display employees whose names start with S.Solution
Solution
NULL Values
UseIS NULL or IS NOT NULL to work with missing values.
Example
Practice
Display employees whose department has been assigned.Solution
Solution
ORDER BY
ORDER BY sorts the result set.
Ascending Order
Descending Order
Multiple Columns
Practice
Display employees sorted by employee name.Solution
Solution
LIMIT and OFFSET
LIMIT
Returns only the specified number of rows.OFFSET
Skips a specified number of rows.Practice
Display the first four employees.Solution
Solution
Aggregate Functions
Aggregate functions perform calculations on multiple rows.Example
Practice
Find the minimum salary.Solution
Solution
GROUP BY
GROUP BY groups rows before performing aggregate calculations.
Example
Practice
Count employees in each city.Solution
Solution
HAVING
HAVING filters groups after aggregation.
Example
Practice
Display cities having more than one employees.Solution
Solution
SQL Execution Order
Although we write queries in this order:WHERE) cannot use column aliases created in SELECT.
Summary
In this chapter, you learned how to:- Retrieve data using
SELECT - Filter rows with
WHERE - Remove duplicates using
DISTINCT - Sort results using
ORDER BY - Limit returned rows using
LIMITandOFFSET - Perform calculations using aggregate functions
- Group data using
GROUP BY - Filter grouped results using
HAVING