What is SQL?
SQL (Structured Query Language)
A standardized programming language specifically designed for managing and manipulating relational databases. SQL allows you to create, read, update, and delete data stored in tables.
Pronounced as "S-Q-L" or "sequel", SQL is the universal language that virtually every database professional and data analyst needs to know.
The Four Core Operations (CRUD)
SQL enables you to perform four fundamental operations on data, commonly known as CRUD:
Create
Add new records to a database table using INSERT statements.
INSERT INTO users
VALUES ('John', 'john@email.com');
Read
Retrieve data from tables using SELECT statements.
SELECT * FROM users
WHERE age > 18;
Update
Modify existing records using UPDATE statements.
UPDATE users
SET email = 'new@email.com'
WHERE id = 1;
Delete
Remove records from tables using DELETE statements.
DELETE FROM users
WHERE status = 'inactive';
SQL Statement Categories
SQL statements are grouped into different categories based on their purpose:
Interactive: Explore SQL Categories
Click to Explore!Click on each category to see its commands and examples:
Data Definition Language (DDL)
Commands that define the structure of the database. Used to create, modify, and delete database objects.
CREATE
ALTER
DROP
TRUNCATE
RENAME
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100));
| Category | Full Name | Purpose | Key Commands |
|---|---|---|---|
| DDL | Data Definition Language | Define database structure | CREATE, ALTER, DROP, TRUNCATE |
| DML | Data Manipulation Language | Manipulate data in tables | SELECT, INSERT, UPDATE, DELETE |
| DCL | Data Control Language | Control access to data | GRANT, REVOKE |
| TCL | Transaction Control Language | Manage transactions | COMMIT, ROLLBACK, SAVEPOINT |
Practice Questions: CRUD Operations
Given:
-- Table: employees
-- Columns: id, name, email, department, salary
Task: Write a SELECT statement to retrieve all columns from the "employees" table.
Hint: Use the asterisk (*) as a wildcard to select all columns.
Show Solution
-- Select all columns from the employees table
SELECT * FROM employees;
The asterisk (*) is a wildcard that selects all columns.
Given:
-- Table: products
-- Columns: id, name, price, stock
Task: Write an INSERT statement to add a new product with name "Laptop" and price 999.99.
Hint: Always specify column names in INSERT for clarity.
Show Solution
-- Insert a new product into the products table
INSERT INTO products (name, price)
VALUES ('Laptop', 999.99);
Always specify column names for clarity and maintainability.
Given:
-- Table: products
-- Contains: id=5, name='Laptop', price=999.99
Task: Write an UPDATE statement to change the price of product with id 5 to 799.99.
Hint: Always use a WHERE clause with UPDATE to avoid modifying all rows!
Show Solution
-- Update the price for a specific product
UPDATE products
SET price = 799.99
WHERE id = 5;
Without WHERE clause, all products would be updated to this price!
Given:
-- User: hr_user
-- Table: employees
-- Required access: SELECT and INSERT only
Task: Write a GRANT statement to give hr_user SELECT and INSERT permissions on the employees table. What SQL category does GRANT belong to?
Hint: GRANT is part of Data Control Language (DCL).
Show Solution
-- GRANT belongs to DCL (Data Control Language)
-- It gives users access privileges to database objects
GRANT SELECT, INSERT ON employees TO hr_user;
DCL commands (GRANT, REVOKE) control who can access what in the database.
Why Learn SQL?
SQL skills are essential for anyone working with data. Here's why you should learn SQL:
High Demand in Job Market
SQL appears in job requirements for Data Analysts, Data Scientists, Backend Developers, Business Analysts, and more. Companies of all sizes need SQL-proficient professionals.
Universal Applicability
Once learned, SQL skills transfer across MySQL, PostgreSQL, SQL Server, Oracle, and other databases. The core syntax remains remarkably consistent.
Foundation for Data Analysis
SQL is the gateway to data analysis. Before using Python, R, or visualization tools, you often need to extract and prepare data with SQL.
Quick to Learn, Powerful Results
You can start writing meaningful queries within days. Advanced mastery enables complex analytics, reporting, and application development.
History and Evolution of SQL
SQL has a rich history spanning over five decades. Understanding its evolution helps appreciate why it remains the standard for relational data management today.
1970 - Relational Model Proposed
Dr. Edgar F. Codd at IBM publishes "A Relational Model of Data for Large Shared Data Banks", laying the theoretical foundation for relational databases.
1974 - SEQUEL Created
IBM researchers Donald Chamberlin and Raymond Boyce develop SEQUEL (Structured English Query Language), which later becomes SQL due to trademark issues.
1979 - Oracle Releases First Commercial SQL
Relational Software Inc. (now Oracle) releases the first commercially available SQL-based RDBMS, making relational databases accessible to businesses.
1986 - SQL Becomes ANSI Standard
The American National Standards Institute (ANSI) adopts SQL as the official standard for relational database languages (SQL-86).
1999-Present - Modern SQL Standards
SQL:1999 adds recursive queries and triggers. SQL:2003 adds XML support. SQL:2016 adds JSON support. SQL continues to evolve with modern data needs.
Major SQL Standard Versions
| Version | Year | Key Features Added |
|---|---|---|
| SQL-86 | 1986 | First ANSI standard, basic query operations |
| SQL-92 | 1992 | JOIN syntax, CASE expressions, string functions |
| SQL:1999 | 1999 | Recursive queries, triggers, procedural extensions |
| SQL:2003 | 2003 | Window functions, XML support, MERGE statement |
| SQL:2016 | 2016 | JSON support, row pattern matching |
Practice Questions: SQL History
Context:
-- SQL is based on a mathematical model
-- proposed in a famous 1970 paper at IBM
Task: Who proposed the relational model that SQL is based on? Name the person and the year.
Hint: The paper was titled "A Relational Model of Data for Large Shared Data Banks".
Show Solution
-- Answer: Dr. Edgar F. Codd at IBM in 1970
-- His paper "A Relational Model of Data for Large
-- Shared Data Banks" founded relational database theory
Given:
-- Table: employees
-- Columns: name, department, salary
Task: Write a query that ranks employees by salary (highest first). What SQL version introduced window functions?
Hint: Use RANK() OVER (ORDER BY ...) syntax.
Show Solution
-- SQL:2003 introduced window functions
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) as rank
FROM employees;
Context:
-- In the late 1970s, a company saw the potential
-- of the relational model and created the first
-- commercial SQL database system
Task: What was the first commercially available SQL database and in what year was it released?
Hint: This company is still one of the leading enterprise database vendors today.
Show Solution
-- Answer: Oracle (1979)
-- Relational Software Inc. (now Oracle Corporation)
-- released the first commercial SQL-based RDBMS
-- Today Oracle is still one of the leading
-- enterprise database systems
Given:
-- Table: customers
-- Column: data (JSON type)
-- Sample data: {"name": "John", "email": "john@example.com", "status": "active"}
Task: Write a query to extract the name and email from the JSON data column for active customers. SQL:2016 introduced JSON support.
Hint: PostgreSQL uses ->> operator, MySQL uses JSON_EXTRACT().
Show Solution
-- PostgreSQL example extracting JSON data
SELECT
data->>'name' as customer_name,
data->>'email' as email
FROM customers
WHERE data->>'status' = 'active';
-- MySQL example
SELECT
JSON_EXTRACT(data, '$.name') as customer_name
FROM customers;
Understanding Relational Databases
A relational database organizes data into tables (also called relations) that can be linked together based on common data elements. This structure makes it easy to understand, maintain, and query data efficiently.
Relational Database
A type of database that stores and provides access to data points that are related to one another. Data is organized into tables with rows (records) and columns (fields), connected through keys.
Key Concepts
Tables
- Collection of related data
- Rows represent records
- Columns represent attributes
- Each cell holds a value
Keys
- Primary Key: Unique identifier
- Foreign Key: Links tables
- Composite Key: Multiple columns
- Candidate Key: Potential PKs
Relationships
- One-to-One (1:1)
- One-to-Many (1:N)
- Many-to-Many (M:N)
- Self-referencing
ACID Properties
Relational databases guarantee data integrity through ACID properties, ensuring reliable transaction processing:
Atomicity
All operations in a transaction succeed or all fail together. No partial updates.
Consistency
Database state remains valid before and after transactions.
Isolation
Concurrent transactions don't interfere with each other.
Durability
Committed changes persist even after system failures.
Interactive: ACID Properties in Action
Click to Explore!Click each property to see real SQL examples and explanations:
Atomicity
All operations in a transaction are completed successfully, or none of them are. There are no partial transactions.
-- Bank transfer example
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
-- Both succeed or both fail
COMMIT;
-- Example: ACID in action with a bank transfer
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1; -- Debit
UPDATE accounts SET balance = balance + 500 WHERE id = 2; -- Credit
-- Both succeed or both fail (Atomicity)
COMMIT; -- Changes are permanent (Durability)
Practice Questions: Relational Databases
Requirements:
-- Table: users
-- id: integer, primary key
-- name: text up to 100 characters, required
-- email: text up to 255 characters, must be unique
Task: Write SQL to create a users table with id, name, and email columns with the constraints specified above.
Hint: Use INT, VARCHAR, PRIMARY KEY, NOT NULL, and UNIQUE constraints.
Show Solution
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE
);
Given:
-- Existing table: users (id INT PRIMARY KEY, ...)
-- New table: orders
-- Columns: order_id, user_id, order_date, total
Task: Create an orders table with a foreign key that references the users table's id column.
Hint: Use FOREIGN KEY (column) REFERENCES table(column) syntax.
Show Solution
CREATE TABLE orders (
order_id INT PRIMARY KEY,
user_id INT,
order_date DATE,
total DECIMAL(10, 2),
FOREIGN KEY (user_id) REFERENCES users(id)
);
Requirements:
-- Table: products
-- product_id: auto-incrementing primary key
-- name: required, up to 200 characters
-- price: required, decimal with 2 decimal places
-- stock: integer, defaults to 0
Task: Create a products table where product_id automatically increments for each new row. Show syntax for both MySQL and PostgreSQL.
Hint: MySQL uses AUTO_INCREMENT, PostgreSQL uses SERIAL.
Show Solution
-- MySQL syntax
CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(200) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT DEFAULT 0
);
-- PostgreSQL syntax
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
name VARCHAR(200) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT DEFAULT 0
);
Scenario:
-- Two users are accessing the same bank account
-- User A: Reading balance
-- User B: Updating balance
-- Problem: User A might see inconsistent data
Task: Which ACID property ensures that concurrent transactions see a consistent database state? Write SQL to set the highest isolation level.
Hint: The property starts with "I" and controls visibility between transactions.
Show Solution
-- Answer: Isolation
-- Isolation ensures concurrent transactions don't
-- interfere with each other
-- Example: Setting transaction isolation level
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- SQL Server example
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SQL vs NoSQL Databases
Understanding when to use SQL (relational) vs NoSQL (non-relational) databases is crucial for modern application development. Each has distinct strengths for different use cases.
SQL Databases
NoSQL Databases
When to Choose Which?
Banking apps, inventory systems, HR databases, and any application requiring complex joins and strict data integrity.
Social media feeds, IoT data, content management, gaming leaderboards, and apps with rapidly changing data structures.
Practice Questions: SQL vs NoSQL
Context:
-- Databases can be categorized as:
-- SQL (Relational): Structured tables with relationships
-- NoSQL (Non-Relational): Flexible schemas, various types
Task: Name two examples of SQL databases and two NoSQL databases. Also identify what type of NoSQL each is (Document, Key-Value, Graph, etc.).
Hint: Think of popular open-source and cloud databases.
Show Solution
-- SQL Databases (Relational):
-- 1. MySQL
-- 2. PostgreSQL
-- Others: SQL Server, Oracle, SQLite
-- NoSQL Databases (Non-Relational):
-- 1. MongoDB (Document)
-- 2. Redis (Key-Value)
-- Others: Cassandra, Neo4j, DynamoDB
Scenario:
-- Application: Online Banking System
-- Requirements:
-- - Money transfers between accounts
-- - Transaction history and reporting
-- - Regulatory compliance and auditing
-- - Multiple concurrent users
Task: Would you use SQL or NoSQL for this banking application? List at least 3 reasons for your choice.
Hint: Consider ACID properties and data integrity requirements.
Show Solution
-- Answer: SQL Database (e.g., PostgreSQL, Oracle)
-- Reasons:
-- 1. ACID compliance ensures transaction integrity
-- 2. Money transfers need atomicity (all or nothing)
-- 3. Complex queries for reports and auditing
-- 4. Strict schema prevents data corruption
-- 5. Regulatory compliance requirements
Popular Database Management Systems
A Database Management System (DBMS) is software that enables users to create, manage, and interact with databases. Here are the most widely used SQL database systems:
MySQL
The world's most popular open-source database. Powers many web applications including Facebook, Twitter, and WordPress.
Strengths
- Easy to learn and use
- Excellent performance
- Strong community support
- Cross-platform compatible
PostgreSQL
The world's most advanced open-source relational database with extensive features and SQL compliance.
Strengths
- Advanced data types (JSON, arrays)
- Full ACID compliance
- Extensible architecture
- Strong data integrity
SQL Server
Microsoft's enterprise-grade database solution with deep integration into the Microsoft ecosystem.
Strengths
- Integration with Azure
- Business Intelligence tools
- High security features
- Excellent documentation
SQLite
Lightweight, file-based database perfect for mobile apps, embedded systems, and local development.
Strengths
- Zero configuration
- Serverless operation
- Cross-platform portable
- Very small footprint
Practice Questions: Database Systems
Scenario:
-- Application: Mobile Note-Taking App
-- Requirements:
-- - Works offline
-- - Minimal setup and configuration
-- - Small memory footprint
-- - Built-in to mobile platforms
Task: Which database would you choose for a mobile app that needs local storage? Provide at least 3 reasons.
Hint: Think about what comes built into Android and iOS.
Show Solution
-- Answer: SQLite
-- Reasons:
-- 1. Lightweight (serverless, file-based)
-- 2. Zero configuration needed
-- 3. Built into Android and iOS
-- 4. Small memory footprint
-- 5. Works offline
Given:
-- Table: users
-- Column: data (JSONB type)
-- Sample: {"name": "Alice", "active": true, "role": "admin"}
Task: Which SQL database is known for the best JSON support? Write a query to select the name where active is true.
Hint: This database uses JSONB (binary JSON) for better performance.
Show Solution
-- Answer: PostgreSQL
-- PostgreSQL has native JSONB type with indexing:
SELECT data->>'name' as name
FROM users
WHERE data @> '{"active": true}';
-- JSONB is binary JSON with better performance
Scenario:
-- Application: Enterprise HR System
-- Tech Stack: .NET Core, C#, Azure Cloud
-- Requirements:
-- - Integration with Azure services
-- - Business Intelligence and reporting
-- - Enterprise security and compliance
Task: What database would be best for a .NET enterprise application on Azure? List 3 reasons.
Hint: Consider Microsoft's ecosystem and cloud offerings.
Show Solution
-- Answer: SQL Server (or Azure SQL Database)
-- Reasons:
-- 1. Native integration with .NET and C#
-- 2. Azure SQL Database for cloud deployment
-- 3. Enterprise features (BI, reporting)
-- 4. Strong security and compliance
-- 5. Microsoft ecosystem integration
Given:
-- Connection details:
-- Host: localhost
-- Database: mydb
-- Username: admin
-- Password: secret
-- MySQL Port: 3306
-- PostgreSQL Port: 5432
Task: Write connection strings for both MySQL and PostgreSQL databases using the details above. Also show how to connect using Python's psycopg2.
Hint: Connection strings follow the format: protocol://user:pass@host:port/database
Show Solution
-- MySQL connection string format:
-- mysql://admin:secret@localhost:3306/mydb
-- PostgreSQL connection string format:
-- postgresql://admin:secret@localhost:5432/mydb
-- Python example with PostgreSQL:
-- import psycopg2
-- conn = psycopg2.connect(
-- host="localhost",
-- database="mydb",
-- user="admin",
-- password="secret"
-- )
Common SQL Data Types
Before diving into queries, it helps to know the common data types you will encounter:
| Category | Data Type | Description | Example |
|---|---|---|---|
| Numeric | INT |
Whole numbers | 42, -100, 0 |
DECIMAL(p,s) |
Exact decimal numbers | 99.99, 1234.56 | |
FLOAT |
Approximate decimals | 3.14159 | |
| Text | VARCHAR(n) |
Variable-length text | 'John', 'Hello World' |
TEXT |
Long text content | Articles, descriptions | |
| Date/Time | DATE |
Date only | '2026-02-06' |
TIMESTAMP |
Date and time | '2026-02-06 14:30:00' | |
| Boolean | BOOLEAN |
True/False values | TRUE, FALSE |
Key Takeaways
Universal Language
SQL is the standard language for relational databases, used by over 60% of developers worldwide
50+ Years Strong
Despite being developed in the 1970s, SQL remains one of the most in-demand tech skills today
ACID Guarantees
Relational databases ensure data integrity through Atomicity, Consistency, Isolation, and Durability
SQL vs NoSQL
Choose SQL for structured data with complex relationships; NoSQL for flexible, scalable applications
CRUD Operations
Master the four core operations: Create (INSERT), Read (SELECT), Update (UPDATE), Delete (DELETE)
Many DBMS Options
MySQL, PostgreSQL, SQL Server, and SQLite serve different needs but share core SQL syntax
Knowledge Check
Test your understanding of SQL fundamentals:
What does SQL stand for?
Which SQL command is used to retrieve data from a database?
What does ACID stand for in database transactions?
Which type of database is MongoDB?
When was the first ANSI SQL standard published?
Which category does the CREATE TABLE command belong to?