Skip to main content

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

The SELECT 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.

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.

DISTINCT

DISTINCT removes duplicate values.

Syntax

Example

Example

Practice

Display all unique department IDs.

WHERE Clause

The WHERE clause filters rows based on a condition.

Syntax

Example

Example

Example

Practice

Display employees earning more than ₹70,000.

Comparison Operators

Example

Practice

Display employees whose salary is less than ₹60,000.

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.

BETWEEN

BETWEEN checks whether a value falls within a range.

Example

Practice

Display employees with salaries between ₹55,000 and ₹80,000.

IN

IN checks whether a value exists in a list.

Example

Practice

Display employees belonging to departments 2 and 3.

LIKE

LIKE searches for text patterns.

Example

Example

Practice

Display employees whose names start with S.

NULL Values

Use IS NULL or IS NOT NULL to work with missing values.

Example

Practice

Display employees whose department has been assigned.

ORDER BY

ORDER BY sorts the result set.

Ascending Order

Descending Order

Multiple Columns

Practice

Display employees sorted by employee name.

LIMIT and OFFSET

LIMIT

Returns only the specified number of rows.

OFFSET

Skips a specified number of rows.

Practice

Display the first four employees.

Aggregate Functions

Aggregate functions perform calculations on multiple rows.

Example

Practice

Find the minimum salary.

GROUP BY

GROUP BY groups rows before performing aggregate calculations.

Example

Practice

Count employees in each city.

HAVING

HAVING filters groups after aggregation.

Example

Practice

Display cities having more than one employees.

SQL Execution Order

Although we write queries in this order:
SQL actually executes them in the following order:
Understanding this order helps explain why some clauses (like 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 LIMIT and OFFSET
  • Perform calculations using aggregate functions
  • Group data using GROUP BY
  • Filter grouped results using HAVING
The next chapter introduces SQL JOINs, allowing you to retrieve related data from multiple tables.