SQL SELECT Statement

SQL Tutorial

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 SELECT statement 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

OperatorMeaning
=Equal
!= or <>Not equal
> <Greater / Less
>= <=Greater/Less equal
BETWEENRange
INMultiple values
LIKEPattern 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

FunctionPurpose
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 rows
  • HAVING → filters groups

SELECT with Aliases (AS)

Column Alias

Table Alias

  •  Improves readability

SELECT with LIKE (Pattern Matching)

PatternMeaning
%Any characters
_Single character

SELECT from Multiple Tables (JOIN Preview)

  •  Used in real-world databases

 Common Mistakes

  • Using HAVING instead of WHERE
  •  Forgetting WHERE in filtering
  •  Using SELECT * everywhere
  •  Wrong column names
  •  Misusing AND / OR

 Interview Questions (Must Prepare)

  1. What is SELECT in SQL?

  2. Difference between WHERE and HAVING

  3. SELECT DISTINCT vs SELECT

  4. Order of execution in SELECT

  5. Difference between LIMIT and TOP

  6. What is aggregate function?


 Execution Order of SELECT (Very Important)

FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
LIMIT
  •  Frequently asked in interviews

 Summary

  • SELECT is used to retrieve data
  •  Supports filtering, sorting, grouping
  •  Works with functions and joins
  •  Core of SQL queries & interviews
  •  Mastering SELECT = mastering SQL

You may also like...