Academic Block

SQL SELECT Get Certified

What is the SELECT Statement?

The SELECT statement is one of the most commonly used SQL commands. It is used to retrieve data from one or more database tables. With SELECT, you can choose specific columns, filter records, sort results, calculate values, and combine data from multiple tables.

Basic SELECT Syntax

What this code does: Retrieves the name and department of all employees.

WITH employees(id, name, department) AS (
  VALUES
    (1, 'Aarav', 'IT'),
    (2, 'Meera', 'Finance'),
    (3, 'Rohan', 'Sales')
)
SELECT name, department
FROM employees;

Selecting a Single Column

What this code does: Returns only the name column from the employees table.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'Sales', 58000)
)
SELECT name
FROM employees;

Selecting Multiple Columns

What this code does: Returns name, department and salary of all employees.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'Sales', 58000)
)
SELECT name, department, salary
FROM employees;

Selecting All Columns

What this code does: Uses * to return every column from the products table.

WITH products(id, name, category, price) AS (
  VALUES
    (1, 'Keyboard', 'Computer', 800),
    (2, 'Mouse', 'Computer', 400),
    (3, 'Monitor', 'Display', 1500)
)
SELECT *
FROM products;

SELECT with WHERE

What this code does: Returns only employees whose salary is greater than 60,000.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'Sales', 58000),
    ('Anika', 'HR', 62000)
)
SELECT name, salary
FROM employees
WHERE salary > 60000;

SELECT with ORDER BY

What this code does: Sorts employees from highest salary to lowest salary.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'Sales', 58000),
    ('Anika', 'HR', 62000)
)
SELECT name, salary
FROM employees
ORDER BY salary DESC;

SELECT DISTINCT

What this code does: Returns only unique department names (removes duplicates).

WITH employees(name, department) AS (
  VALUES
    ('Aarav', 'IT'),
    ('Meera', 'Finance'),
    ('Rohan', 'IT'),
    ('Anika', 'HR'),
    ('Vikram', 'Finance')
)
SELECT DISTINCT department
FROM employees;

SELECT with Calculations

What this code does: Calculates total cost (price × quantity) for each product.

WITH products(name, price, quantity) AS (
  VALUES
    ('Keyboard', 800, 2),
    ('Mouse', 400, 3),
    ('Monitor', 1500, 1)
)
SELECT name,
       price,
       quantity,
       price * quantity AS total_cost
FROM products;

SELECT with Column Aliases

What this code does: Renames the columns in the result using the AS keyword.

WITH employees(name, salary) AS (
  VALUES
    ('Aarav', 65000),
    ('Meera', 72000),
    ('Rohan', 58000)
)
SELECT name AS employee_name,
       salary AS annual_salary
FROM employees;

SELECT with LIMIT

What this code does: Returns only the top 3 most expensive products.

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500),
    ('Webcam', 1000),
    ('Speaker', 2200)
)
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;

SELECT with IN

What this code does: Returns employees who work in either the IT or Finance department.

WITH employees(name, department) AS (
  VALUES
    ('Aarav', 'IT'),
    ('Meera', 'Finance'),
    ('Rohan', 'Sales'),
    ('Anika', 'HR')
)
SELECT name, department
FROM employees
WHERE department IN ('IT', 'Finance');

SELECT with BETWEEN

What this code does: Finds products whose price is between 500 and 1600 (inclusive).

WITH products(name, price) AS (
  VALUES
    ('Mouse', 400),
    ('Keyboard', 800),
    ('Monitor', 1500),
    ('Speaker', 2500)
)
SELECT name, price
FROM products
WHERE price BETWEEN 500 AND 1600;

SELECT with LIKE

What this code does: Finds all employee names that start with the letter “A”.

WITH employees(name) AS (
  VALUES
    ('Aarav'),
    ('Anika'),
    ('Rohan'),
    ('Meera')
)
SELECT name
FROM employees
WHERE name LIKE 'A%';

SELECT with NULL Values

What this code does: Finds employees who do not have a manager assigned (NULL value).

WITH employees(name, manager) AS (
  VALUES
    ('Aarav', 'Ravi'),
    ('Meera', NULL),
    ('Rohan', 'Priya')
)
SELECT name, manager
FROM employees
WHERE manager IS NULL;

SELECT with Aggregate Functions

What this code does: Calculates the total number of orders, total sales amount, and average sale.

WITH sales(amount) AS (
  VALUES
    (1200),
    (800),
    (1500),
    (700)
)
SELECT COUNT(*) AS total_orders,
       SUM(amount) AS total_sales,
       AVG(amount) AS average_sale
FROM sales;

SELECT with GROUP BY

What this code does: Counts how many employees work in each department.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'IT', 72000),
    ('Rohan', 'HR', 58000),
    ('Anika', 'HR', 62000)
)
SELECT department,
       COUNT(*) AS employee_count
FROM employees
GROUP BY department;

SELECT with HAVING

What this code does: Shows only departments that have more than 2 employees.

WITH employees(name, department) AS (
  VALUES
    ('Aarav', 'IT'),
    ('Meera', 'IT'),
    ('Rohan', 'IT'),
    ('Anika', 'HR'),
    ('Vikram', 'HR')
)
SELECT department,
       COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;

SELECT from Multiple Tables

What this code does: Joins employees with departments to show each employee’s department name.

WITH employees(id, name, department_id) AS (
  VALUES
    (1, 'Aarav', 10),
    (2, 'Meera', 20),
    (3, 'Rohan', 10)
),
departments(id, department_name) AS (
  VALUES
    (10, 'IT'),
    (20, 'Finance')
)
SELECT e.name,
       d.department_name
FROM employees AS e
JOIN departments AS d
  ON e.department_id = d.id;

Selecting a Constant Value

What this code does: Returns a constant string and a calculated value without using any table.

SELECT 'Hello, SQL!' AS message,
       10 + 5 AS result;

Common SELECT Clauses

Clause
Purpose
Example
SELECT
Choose columns or expressions
SELECT name
FROM
Specify the data source
FROM employees
WHERE
Filter rows
WHERE salary > 60000
GROUP BY
Group rows
GROUP BY department
HAVING
Filter grouped results
HAVING COUNT(*) > 2
ORDER BY
Sort results
ORDER BY salary DESC
LIMIT
Restrict rows in SQLite
LIMIT 5

Order of a SELECT Query

What this code does: Demonstrates a complete SELECT query with WHERE, GROUP BY, HAVING, ORDER BY and LIMIT.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'IT', 72000),
    ('Rohan', 'HR', 58000),
    ('Anika', 'HR', 62000)
)
SELECT department,
       COUNT(*) AS employee_count,
       AVG(salary) AS average_salary
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING COUNT(*) > 0
ORDER BY average_salary DESC
LIMIT 10;

Advantages of SELECT

  • Retrieves only the data you need.
  • Can filter and sort query results.
  • Can remove duplicate values using DISTINCT.
  • Can perform calculations and aggregations.
  • Can retrieve data from multiple related tables.
  • Forms the foundation of most SQL data-retrieval queries.

Best Practices

  • Specify only the columns you need instead of using SELECT * when possible.
  • Use meaningful aliases for calculated or renamed columns.
  • Use WHERE to filter rows before returning unnecessary data.
  • Use ORDER BY when the order of results matters.
  • Use LIMIT when you only need a specific number of rows in SQLite.
  • Use DISTINCT only when duplicate results actually need to be removed.
  • Use table aliases to make multi-table queries easier to read.

The SELECT statement is the foundation of SQL data retrieval. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.