SQL

What is SQL?

SQL is a domain-specific language that is designed to work in a specific domain, database management, unlike general-purpose languages like Python or JavaScript. It's like how HTML is used only for web structure, or CSS for styling. SQL lets you create, read, update, and delete (often referred to as CRUD) data from a relational database.

SQL is globally standardized, but in practice, its syntax and features can vary between different DBMS like MySQL, PostgreSQL, SQLite, or Microsoft SQL Server. The core structure remains the same, but each system may add its own twists and extensions.

Syntax

In SQL, the semicolon (;) marks the end of a query. This allows you to write multiple queries on different lines, all to be run in sequence once a semicolon is hit. For example:

SELECT * FROM users;
SELECT * FROM orders;

Here's a quick overview of commonly used SQL commands and patterns:

Task Syntax
Query(single table) SELECT x FROM y;
SELECT x FROM y ORDER BY z;
Query(multiple tables) SELECT x FROM y INNER JOIN z ON q;
Alias SELECT x AS q FROM y;
Grouping SELECT x FROM y GROUP BY z;
Aggregates SELECT COUNT(x) FROM y;
MIN(), MAX(), SUM(), AVG(),

Filtering:

SELECT x FROM y WHERE z = "foobar";
WHERE z = "foo" AND q != "bar";
OR
SELECT x FROM y WHERE z LIKE "_oobar";
WHERE z BETWEEN 1 AND 100;
IS NOT NULL;
SELECT x FROM y WHERE z IN ("foo","bar","foobar");