View Categories

SQL Basics

Introduction to Databases (RDBMS) #

What is a Database? #

A database is a structured collection of data stored electronically.

Example:

  • Customer records
  • Sales data
  • Employee data

What is RDBMS? #

RDBMS (Relational Database Management System) stores data in tables (rows & columns) and allows relationships between them.

Key Concepts #

ConceptMeaning
TableCollection of data
RowSingle record
ColumnAttribute/field
Primary KeyUnique identifier
Foreign KeyLink between tables

Example Table #

IDNameAgeCity
1Alex25London
2John30New York

What is SQL? #

SQL (Structured Query Language) is used to:

  • Retrieve data
  • Filter data
  • Sort data
  • Manage databases

SELECT Statement #

What is SELECT? #

Used to retrieve data from a table

Syntax #

SELECT column_name FROM table_name;

Examples #

Select all columns: #

SELECT * FROM Students;

Select specific columns:

SELECT Name, Age FROM Students;

FROM Clause #

What is FROM? #

Specifies which table to retrieve data from

Example #

SELECT Name FROM Students;

Students is the table

WHERE Clause #

What is WHERE? #

Used to filter data based on conditions

Syntax #

SELECT column FROM table
WHERE condition;

Examples #

Filter by age: #

SELECT * FROM Students
WHERE Age > 25;

Filter by text:

SELECT * FROM Students
WHERE City = 'London';

Operators #

OperatorMeaning
=Equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal
!=Not equal

DISTINCT #

What is DISTINCT? #

Removes duplicate values

Syntax #

SELECT DISTINCT column FROM table;

Example

SELECT DISTINCT City FROM Students;

Shows unique cities only

ORDER BY #

What is ORDER BY? #

Used to sort data

Syntax #

SELECT column FROM table
ORDER BY column ASC|DESC;

Examples #

Ascending (default): #

SELECT * FROM Students
ORDER BY Age ASC;

Descending:

SELECT * FROM Students
ORDER BY Age DESC;

LIMIT #

What is LIMIT? #

Used to restrict number of rows returned

Syntax #

SELECT column FROM table
LIMIT number;

8.3 Example

SELECT * FROM Students
LIMIT 3;

Returns first 3 rows

Combined Query Example #

SELECT DISTINCT City
FROM Students
WHERE Age > 25
ORDER BY City ASC
LIMIT 5;

This query:

  • Removes duplicates
  • Filters age > 25
  • Sorts cities
  • Returns top 5 results

Execution Order (Important) #

SQL runs in this order:

  1. FROM
  2. WHERE
  3. SELECT
  4. DISTINCT
  5. ORDER BY
  6. LIMIT

Table #

ClausePurpose
SELECTChoose columns
FROMSpecify table
WHEREFilter data
DISTINCTRemove duplicates
ORDER BYSort data
LIMITRestrict rows

RDBMS → Stores data in tables SQL → Language to query data Core clauses:

  • SELECT → retrieve
  • FROM → source
  • WHERE → filter
  • DISTINCT → unique values
  • ORDER BY → sort
  • LIMIT → restrict results
SQL Basics
💬
AIRA (AI Research Assistant) Neural Learning Interface • Drag & Resize Enabled
×