Programming languages
SQL
The standard declarative query language for relational databases: selecting, changing and defining data.
What it is
SQL grew out of IBM research in the 1970s on Edgar Codd's relational model. The first ANSI standard came in 1986, followed by ISO/IEC 9075. New editions still appear (SQL:2016, SQL:2023).
The language is declarative: you describe what data you need, and the database planner decides how to get it. The main command groups are DQL (SELECT), DML (INSERT, UPDATE, DELETE), DDL (CREATE, ALTER) and transaction control.
Every database (PostgreSQL, MySQL, SQLite, SQL Server, Oracle) has its own dialect with extensions, but the core is shared. Modern SQL supports window functions, CTEs, recursive queries and JSON.
Use it when
- Structured data with relationships: users, orders, payments.
- Analytics and reports: aggregations, grouping, window functions.
- You need transactions and data integrity.
- Several applications work with the same data.
Avoid it when
- Loosely structured documents with a changing schema, where a document database fits better.
- Complex processing logic that belongs in application code, not in hundreds of lines of SQL.
Key concepts
- JOIN
- Combines rows from several tables by a condition:
INNER,LEFT,RIGHT,FULL. - GROUP BY and aggregates
- Groups rows and computes
COUNT,SUM,AVG.HAVINGfilters groups. - Index
- A structure that speeds up lookups but slows down writes.
- Transaction (ACID)
- A group of operations either completes fully or not at all.
- Window functions and CTE
OVER (...)computes over a window of rows,WITHgives named subqueries.
Example
Interview questions
1. How do INNER JOIN and LEFT JOIN differ?
INNER JOINreturns only rows that match in both tables.LEFT JOINreturns all rows of the left table and fills the right side with NULL where there is no match.2. WHERE or HAVING?
WHEREfilters rows before grouping,HAVINGfilters grouped results, for example bySUMorCOUNT.3. What is an index and when does it hurt?
An index (usually a B-tree) speeds up lookups and sorting. But it takes space and slows
INSERT/UPDATE, so unneeded indexes hurt.4. What is a transaction and ACID?
A transaction is an atomic group of operations. ACID means atomicity, consistency, isolation, durability. It prevents half-applied data after a failure.
Interview questions from the bank
- What does
DISTINCTdo and how is it different fromGROUP BY?JuniorSQL - How do
ORDER BYandLIMITwork, and why isLIMITwithoutORDER BYdangerous?JuniorSQL - What is the difference between
UNIONandUNION ALL?JuniorSQL - What is the difference between
DELETE,TRUNCATEandDROP TABLE?JuniorSQL - What is a subquery and how does
EXISTSdiffer fromIN?JuniorSQL - What are
CASEandCOALESCEfor?JuniorSQL - What constraints exist in SQL and why put them in the database rather than only in code?JuniorSQL
- What is a
VIEWand how is it different from a materialized view?JuniorSQL
Related technologies
- PostgreSQLA powerful open source relational database with extensions, JSONB, full-text search and strong SQL support.
- MySQLOne of the most widespread relational databases, the foundation of many web projects and the LAMP stack.
- SQLiteAn embedded serverless database in a single file: the most widely deployed database in the world.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).