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


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;
- Specifies columns to retrieve or uses
*for all columns. - Retrieves all rows unless filtered with conditions.
Q: What are the key components of a SELECT statement?
- SELECT: Specifies the columns to retrieve (e.g.,
FirstName,Salary). - FROM: Identifies the table(s) to query (e.g.,
Employees). - Optional Clauses:
WHERE:Filters rows based on conditions.DISTINCT:Removes duplicate rows.ORDER BY:Sorts results.LIMIT/TOP:Restricts the number of rows returned.
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

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

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:
- Listing unique departments in a company.
- Removing duplicate entries from query results.
- Caution: Overusing
DISTINCTcan impact performance, so use it only when necessary.
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

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

Q: What are LIMIT and TOP in SQL?
- LIMIT: Restricts the number of rows returned, commonly used in MySQL, PostgreSQL, and SQLite.
- TOP: Similar to
LIMIT, used in SQL Server. - Some RDBMS support additional clauses like
OFFSET(withLIMIT) to skip rows orWITH TIES(withTOP) to include tied values.
Syntax: SELECT column1 FROM table_name LIMIT n; (returns top n rows).
Syntax: SELECT TOP n column1 FROM table_name;
Q: When should you use LIMIT or TOP?
Use LIMIT/TOP for:
- Displaying a subset of results (e.g., top 5 employees by salary).
- Paginating results (e.g.,
LIMIT 10 OFFSET 20for page 3). - Improving query performance on large datasets.
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

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

Q: What are common mistakes with basic SQL queries?
SELECT:
- Using
SELECT *in production, which can slow queries and return unnecessary data. - Misspelling column names, causing errors (especially in case-sensitive RDBMS like PostgreSQL).
WHERE:
- Omitting
WHEREinSELECT, returning too many rows. - Using incorrect operators (e.g.,
=instead ofLIKEfor partial string matches).
DISTINCT:
- Overusing
DISTINCTto mask poor data design, impacting performance. - Applying
DISTINCTto multiple columns without understanding it affects entire rows.
ORDER BY:
- Forgetting
ASC/DESC, assuming default sort order (usuallyASC). - Sorting on non-indexed columns, slowing queries on large datasets.
LIMIT/TOP:
- Using
LIMITwithoutORDER BY, leading to unpredictable results. - Confusing
LIMITsyntax across RDBMS (e.g., usingLIMITin SQL Server, which requiresTOP).
Q: What are best practices for basic SQL queries?
SELECT Statement:
- Specify exact columns instead of
SELECT *for performance and clarity. - Use table aliases (e.g.,
SELECT e.FirstName FROM Employees e) for readability in complex queries.
Retrieving Columns and Rows:
- Use
WHEREto filter early, reducing data processed. - Validate conditions with appropriate operators (e.g.,
LIKEfor strings,INfor lists).
DISTINCT:
- Use
DISTINCTsparingly, only when duplicates are expected. - Consider normalizing data to avoid duplicates at the source.
ORDER BY:
- Always specify
ASCorDESCfor clarity. - Sort on indexed columns for better performance.
- Use multiple columns in
ORDER BYfor precise sorting.
LIMIT/TOP:
- Combine with
ORDER BYto ensure predictable results. - Use
OFFSETfor pagination, but test performance on large datasets. - Verify RDBMS-specific syntax (
LIMITfor MySQL/PostgreSQL,TOPfor SQL Server).
General:
- Write readable queries with consistent formatting (e.g., uppercase keywords, aligned clauses).
- Comment complex queries (e.g.,
-- Filter IT employees) for maintenance. - Test queries on small datasets before running on large tables.
- Use indexes on frequently filtered or sorted columns to optimize performance.