ReviseAlgo Logo

Course Overview

What is SQL?

An intuitive introduction to Structured Query Language, starting with non-technical analogies and moving to its formal technical definition.

Interview: Commonly asked in entry-level engineering interviews to evaluate your baseline database knowledge and relational query understanding.

Last Updated: June 14, 2026 15 min read

1. Introduction

SQL (Structured Query Language) is a domain-specific programming language designed for managing, querying, and manipulating data stored in a Relational Database Management System (RDBMS). Unlike general-purpose languages such as Java or Python, SQL is declarative — you describe what data you want, and the database engine determines the most efficient way to retrieve it.

Whether you are building web applications, analyzing business metrics, or designing backend systems, SQL is the universal language for interacting with structured data. This topic walks you through why SQL was created, how it works internally, and why it remains the most in-demand technical skill in software engineering.

2. Why It Matters

Before SQL existed, data was stored in flat files and hierarchical databases where programmers had to write hundreds of lines of procedural code just to retrieve a simple list of records. When the data structure changed, every program broke.

SQL solved this by separating what data you need from how the database retrieves it. This separation — called data independence — means you can restructure the entire storage layer without changing a single query. Today, over 80% of production database systems worldwide are relational databases using SQL as their primary interface.

  • Backend engineers write SQL daily to build APIs, design schemas, and debug performance bottlenecks.
  • Data engineers use SQL to build ETL pipelines, transform datasets, and power analytics dashboards.
  • Product managers and business analysts run SQL queries directly to answer business questions without waiting for engineering support.

3. Real-World Analogy

Imagine a massive warehouse with millions of packages. Without an inventory system, finding all blue electronics shipped in March means opening every single box. With a proper catalog system, you simply ask the warehouse manager: “Show me all blue electronics shipped in March, sorted by date.” The manager knows exactly where to look.

In this analogy: the warehouse is the database, the catalog system is the RDBMS, and the question you asked is a SQL query. SQL is the standardized language you use to communicate your data needs — just like speaking the warehouse manager’s language to get exactly what you need, instantly.

4. How It Works

SQL is built on the relational model proposed by Edgar F. Codd in 1970. The core idea: all data is stored in tables (relations) consisting of rows (tuples) and columns (attributes). Key principles include:

  • Declarative syntax: You specify what result you want (e.g., “all customers from New York”), not the step-by-step algorithm to find them.
  • Set-based operations: SQL operates on entire sets of rows at once, unlike procedural languages that process records one at a time.
  • Data independence: The logical schema (tables, columns) is separate from the physical storage (disk layout, indexes).
  • Mathematical foundation: Every SQL query can be expressed as operations in relational algebra (selection, projection, join, union), enabling formal optimization.

5. Internal Architecture

When you execute a SQL query in PostgreSQL, it passes through four distinct phases before returning results:

The query planner is the most critical component — it uses table statistics, index metadata, and cost models to choose the fastest execution strategy. Two functionally identical queries can have wildly different performance depending on the plan chosen.

6. Visual Explanation

The diagram above illustrates the evolution from file-based storage through hierarchical databases to the modern relational model, showing how SQL emerged as the universal data interface.

7. Practical Example

Here are progressively complex SQL queries demonstrating the declarative power of the language:

Notice how each query describes the desired result without specifying how to scan tables, which indexes to use, or how to sort data — the database engine handles all of that internally.

8. Common Mistakes

  • Treating all SQL databases the same: PostgreSQL, MySQL, Oracle, and SQL Server all implement the ANSI SQL standard differently. Window function syntax, string functions, and date handling vary across vendors.
  • Forgetting that SQL is declarative: Beginners often try to write SQL like procedural code (loops, step-by-step logic). Think in sets, not iterations.
  • Ignoring NULL semantics: In SQL, NULL = NULL evaluates to UNKNOWN, not TRUE. Use IS NULL instead of = NULL.
  • SELECT * in production: Always list specific columns. SELECT * breaks when columns are added and wastes network bandwidth.

9. Quick Quiz

Q1: What type of programming language is SQL?

A) Procedural — it executes instructions step by step
B) Declarative — it describes what result you want
C) Object-oriented — it uses classes and objects
D) Functional — it chains pure functions

Answer: B) Declarative

Q2: Who proposed the relational model that SQL is based on?

A) Alan Turing
B) Donald Knuth
C) Edgar F. Codd
D) Linus Torvalds

Answer: C) Edgar F. Codd (1970)

Q3: What does the query planner do?

A) Validates SQL syntax
B) Chooses the most efficient execution strategy using statistics and cost models
C) Creates indexes automatically
D) Converts SQL to machine code

Answer: B) Chooses the most efficient execution strategy

10. Scenario-Based Challenge

Challenge: The Startup’s First Database

You’re the first engineer at a startup that currently stores all user data in JSON files. The product has 50,000 users and growing. The CEO asks: “Why should we migrate to a SQL database? JSON works fine!”

Write a 3-point argument covering: (1) ACID guarantees for payments, (2) ad-hoc querying without writing code, (3) concurrent access safety. Then sketch a simple two-table schema (users + orders) that demonstrates referential integrity.

11. Debugging Exercise

The following query is supposed to find all products in the “Electronics” category, but returns zero rows. Can you spot the bug?

Fix: Use ILIKE for case-insensitive matching, or wrap with UPPER(category) = 'ELECTRONICS'.

12. Interview Questions

Q: What is the difference between SQL and a procedural language like Java?

A: SQL is declarative — you specify what data to retrieve, not how to retrieve it. Java is procedural — you write step-by-step instructions. The SQL query optimizer handles execution planning transparently.

Q: Why do we need SQL if NoSQL databases exist?

A: SQL databases provide ACID transactions, strong consistency, complex joins, and standardized querying — essential for financial systems, e-commerce, and any application where data correctness is non-negotiable. NoSQL excels at specific workloads (caching, document storage) but doesn’t replace SQL’s guarantees.

Q: Name the four phases of PostgreSQL query execution.

A: (1) Parsing — syntax validation and parse tree construction, (2) Rewriting — view expansion and rule application, (3) Planning/Optimization — cost-based strategy selection, (4) Execution — running the plan and returning results.

13. Production Considerations

  • Connection management: Each database connection consumes memory and file descriptors. Use connection pools (PgBouncer, HikariCP) to limit concurrent connections to 20–50 per application instance.
  • Query performance monitoring: Enable pg_stat_statements to track slow queries. Set alerts for queries exceeding p99 latency thresholds (typically 100ms for OLTP workloads).
  • Schema migrations: Use versioned migration tools (Flyway, golang-migrate) instead of ad-hoc DDL. Every schema change should be reviewed, tested, and reversible.
  • Security: Always use parameterized queries to prevent SQL injection. Implement row-level security (RLS) for multi-tenant applications. Restrict database user permissions to the minimum required.
  • Backup and recovery: Configure both full backups (pg_dump) and continuous WAL archiving (pg_basebackup + WAL-E). Test restores quarterly — untested backups are not backups.