SQL SELECT Statement Basics: Master Retrieving Columns/Rows, DISTINCT, ORDER BY, LIMIT/TOP with Examples & Best Practices

Table of Contents

1. SELECT Statement Basics

SQL SELECT Statement Basics Flowchart and Components Diagram

SQL SELECT Query Logical Processing Order Diagram

Q: What is the SELECT statement in SQL?

The SELECT statement is used to retrieve data from one or more tables in a relational database. It is the core of SQL's Data Manipulation Language (DML) for querying data. The basic syntax is:

SELECT column1, column2, ... FROM table_name;

Q: What are the key components of a SELECT statement?

Q: Can you give an example of a basic SELECT statement?

-- Create a sample table
CREATE TABLE Employees (
  EmployeeID INT PRIMARY KEY,
  FirstName VARCHAR(50),
  LastName VARCHAR(50),
  Department VARCHAR(50),
  Salary DECIMAL(10, 2)
);

-- Insert sample data
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary)
VALUES
  (1, 'John', 'Doe', 'IT', 60000.00),
  (2, 'Jane', 'sahil', 'HR', 55000.00),
  (3, 'kristal', 'Johnson', 'IT', 65000.00),
  (4, 'Ram', 'Williams', 'HR', 55000.00);

-- Basic SELECT statement
SELECT FirstName, LastName, Salary
FROM Employees;

Output:

FirstName | LastName | Salary
----------|-----------|--------
John      | Doe      | 60000.00
Jane      | sahil    | 55000.00
kristal   | Johnson  | 65000.00
Ram       | Williams | 55000.00

2. Retrieving Columns and Rows

SQL SELECT Specific Columns and WHERE Row Filtering Diagram

Q: How do you retrieve specific columns in SQL?

Use the SELECT statement with the desired column names, separated by commas. To retrieve all columns, use SELECT *.

Example: SELECT FirstName, Salary FROM Employees; retrieves only FirstName and Salary.

Best Practice: Specify columns explicitly instead of SELECT * for better performance and clarity.

Q: How do you retrieve specific rows in SQL?

Use the WHERE clause to filter rows based on conditions. Common operators include =, >, <, LIKE, and IN.

Example: SELECT * FROM Employees WHERE Department = 'IT'; retrieves only IT department employees.

Q: Can you give an example of retrieving specific columns and rows?

-- Retrieve specific columns and rows
SELECT FirstName, LastName, Department
FROM Employees
WHERE Salary > 56000;

Output:

FirstName | LastName | Department
----------|----------|-----------
John      | Doe      | IT
kristal   | Johnson  | IT

3. Using DISTINCT to Remove Duplicates

SQL DISTINCT Keyword Removing Duplicates Visual Explanation

Q: What is the DISTINCT keyword in SQL?

The DISTINCT keyword eliminates duplicate rows from the result set, based on the selected columns. It is placed after SELECT and applies to the entire row or specified columns.

Syntax: SELECT DISTINCT column1, column2 FROM table_name;

Q: When should you use DISTINCT?

Use DISTINCT when you need unique values, such as:

Q: Can you give an example of using DISTINCT?

-- Retrieve unique departments
SELECT DISTINCT Department
FROM Employees;

Output:

Department
----------
IT
HR

4. Sorting Results with ORDER BY

SQL ORDER BY Clause Ascending/Descending Sorting Illustration

Q: What is the ORDER BY clause in SQL?

The ORDER BY clause sorts the result set based on one or more columns, in ascending (ASC) or descending (DESC) order. It is placed at the end of the SELECT statement.

Syntax: SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC];

Q: How does ORDER BY work with multiple columns?

You can specify multiple columns in ORDER BY, and SQL sorts by the first column, then the second, and so on.

Example: ORDER BY Department ASC, Salary DESC sorts by Department ascending, then Salary descending within each department.

Q: Can you give an example of ORDER BY?

-- Sort employees by Salary (descending) and FirstName (ascending)
SELECT FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC, FirstName ASC;

Output:

FirstName | LastName | Salary
----------|----------|--------
kristal   | Johnson  | 65000.00
John      | Doe      | 60000.00
Ram       | Williams | 55000.00
Jane      | sahil    | 55000.00

5. Limiting Results with LIMIT/TOP

SQL LIMIT/TOP and OFFSET Pagination Diagram

Q: What are LIMIT and TOP in SQL?

Q: When should you use LIMIT or TOP?

Use LIMIT/TOP for:

Q: Can you give an example of LIMIT and TOP?

-- Using LIMIT (MySQL, PostgreSQL, SQLite)
SELECT FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC
LIMIT 2;

-- Using TOP (SQL Server)
SELECT TOP 2 FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC;

-- Using LIMIT with OFFSET (e.g., for pagination)
SELECT FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC
LIMIT 2 OFFSET 2;

Output (LIMIT 2):

FirstName | LastName | Salary
----------|----------|--------
kristal   | Johnson  | 65000.00
John      | Doe      | 60000.00

Output (LIMIT 2 OFFSET 2):

FirstName | LastName  | Salary
----------|-----------|--------
Ram       | Williams  | 55000.00
Jane      | sahil     | 55000.00

6. Comprehensive Example Combining All Concepts

Comprehensive SQL SELECT Example with DISTINCT, ORDER BY, LIMIT

Q: Can you provide a comprehensive example using SELECT, DISTINCT, ORDER BY, and LIMIT/TOP?

-- Create sample table
CREATE TABLE Employees (
  EmployeeID INT PRIMARY KEY,
  FirstName VARCHAR(50),
  LastName VARCHAR(50),
  Department VARCHAR(50),
  Salary DECIMAL(10, 2)
);

-- Insert sample data
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary)
VALUES
  (1, 'John', 'Doe', 'IT', 60000.00),
  (2, 'Jane', 'sahil', 'HR', 55000.00),
  (3, 'kristal', 'Johnson', 'IT', 65000.00),
  (4, 'Ram', 'Williams', 'HR', 55000.00),
  (5, 'hari', 'Brown', 'IT', 65000.00);

-- Query 1: Retrieve specific columns with WHERE
SELECT FirstName, LastName, Department, Salary
FROM Employees
WHERE Department = 'IT';

-- Query 2: Use DISTINCT to get unique salaries
SELECT DISTINCT Salary
FROM Employees;

-- Query 3: Sort by Department and Salary
SELECT FirstName, LastName, Department, Salary
FROM Employees
ORDER BY Department ASC, Salary DESC;

-- Query 4: Limit to top 2 employees by salary (MySQL/PostgreSQL/SQLite)
SELECT FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC
LIMIT 2;

-- Query 5: Top 2 employees by salary (SQL Server)
SELECT TOP 2 FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC;

Output (Query 1 - WHERE):

FirstName | LastName | Department | Salary
----------|----------|------------|--------
John      | Doe      | IT         | 60000.00
kristal   | Johnson  | IT         | 65000.00
hari      | Brown    | IT         | 65000.00

Output (Query 2 - DISTINCT):

Salary
--------
55000.00
60000.00
65000.00

Output (Query 3 - ORDER BY):

FirstName | LastName  | Department | Salary
----------|-----------|------------|--------
Ram       | Williams  | HR         | 55000.00
Jane      | sahil     | HR         | 55000.00
kristal   | Johnson   | IT         | 65000.00
hari      | Brown     | IT         | 65000.00
John      | Doe       | IT         | 60000.00

Output (Query 4/5 - LIMIT/TOP):

FirstName | LastName | Salary
----------|----------|--------
kristal   | Johnson  | 65000.00
hari      | Brown    | 65000.00

7. Common Mistakes and Best Practices

SQL SELECT Statement Best Practices Infographic: Avoid SELECT *, Use Aliases

Q: What are common mistakes with basic SQL queries?

SELECT:

WHERE:

DISTINCT:

ORDER BY:

LIMIT/TOP:

Q: What are best practices for basic SQL queries?

SELECT Statement:

Retrieving Columns and Rows:

DISTINCT:

ORDER BY:

LIMIT/TOP:

General: