SQL SELECT Statement

SQL SELECT Statement (Complete Guide: Beginner → Advanced)
The SQL SELECT Statement is the most important SQL command.
It is used to retrieve data from one or more tables in a database.
What is SELECT?
The
SELECTstatement is used to fetch data from a database table.
Select All Columns (SELECT *)
- Retrieves all columns and all rows
- Avoid in large tables (performance issue)
Select Specific Columns
- Faster
- Cleaner
- Recommended practice
SELECT with WHERE Clause
Used to filter records.
Common Operators
| Operator | Meaning |
|---|---|
= | Equal |
!= or <> | Not equal |
> < | Greater / Less |
>= <= | Greater/Less equal |
BETWEEN | Range |
IN | Multiple values |
LIKE | Pattern matching |
SELECT with AND, OR, NOT
SELECT DISTINCT
Removes duplicate values.
- Returns unique values only
SELECT with ORDER BY
Used to sort results.
ASC→ ascending (default)DESC→ descending
SELECT with LIMIT / TOP
MySQL / PostgreSQL / SQLite
SQL Server
- Useful for pagination
SELECT with Aggregate Functions
| Function | Purpose |
|---|---|
COUNT() | Count rows |
SUM() | Total |
AVG() | Average |
MIN() | Minimum |
MAX() | Maximum |
SELECT with GROUP BY
Used with aggregate functions.
- Groups rows by column
SELECT with HAVING
Used to filter groups, not rows.
WHERE→ filters rowsHAVING→ filters groups
SELECT with Aliases (AS)
Column Alias
Table Alias
- Improves readability
SELECT with LIKE (Pattern Matching)
| Pattern | Meaning |
|---|---|
% | Any characters |
_ | Single character |
SELECT from Multiple Tables (JOIN Preview)
- Used in real-world databases
Common Mistakes
- Using
HAVINGinstead ofWHERE ForgettingWHEREin filtering- Using
SELECT *everywhere - Wrong column names
- Misusing
AND/OR
Interview Questions (Must Prepare)
What is
SELECTin SQL?Difference between
WHEREandHAVINGSELECT DISTINCTvsSELECTOrder of execution in
SELECTDifference between
LIMITandTOPWhat is aggregate function?
Execution Order of SELECT (Very Important)
- Frequently asked in interviews
Summary
SELECTis used to retrieve data- Supports filtering, sorting, grouping
- Works with functions and joins
- Core of SQL queries & interviews
- Mastering
SELECT= mastering SQL
