nimbuscode.dev/technologies/sql
C:\> cat TECHNOLOGIES/SQL.md
Loading SQL documentation...

SQL

Query Language

1. Introduction

SQL (Structured Query Language) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS). SQL became a standard of the American National Standards Institute (ANSI) in 1986 and of the International Organization for Standardization (ISO) in 1987.

SQL allows users to access, manipulate, and retrieve data stored in relational databases. It provides a declarative approach where users specify what data they want, rather than how to get it. SQL is the foundation for most relational database operations and is implemented in various database systems including MySQL, PostgreSQL, SQLite, Microsoft SQL Server, and Oracle Database.

2. Syntax Examples

Basic Queries: SELECT, FROM, WHERE
-- Select all columns from a table
SELECT *
FROM customers;

-- Select specific columns
SELECT first_name, last_name, email
FROM customers;

-- Filtering with WHERE clause
SELECT product_name, price, stock
FROM products
WHERE price > 50;

-- Multiple conditions with AND/OR
SELECT *
FROM orders
WHERE order_date >= '2024-01-01'
  AND (status = 'shipped' OR status = 'delivered');
Data Manipulation: INSERT, UPDATE, DELETE
-- Insert a new row
INSERT INTO customers (first_name, last_name, email, phone)
VALUES ('John', 'Doe', '[email protected]', '555-123-4567');

-- Insert multiple rows
INSERT INTO products (product_name, price, category_id)
VALUES 
    ('Laptop', 899.99, 1),
    ('Smartphone', 499.99, 1),
    ('Headphones', 79.99, 2);

-- Update existing records
UPDATE products
SET price = price * 0.9
WHERE category_id = 3;

-- Delete records
DELETE FROM orders
WHERE order_date < '2023-01-01'
  AND status = 'completed';
Advanced Features: JOINs, Aggregation, Subqueries
-- INNER JOIN between tables
SELECT o.order_id, c.first_name, c.last_name, o.order_date
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2024-01-01';

-- LEFT JOIN (keeps all rows from left table)
SELECT p.product_name, c.category_name
FROM products p
LEFT JOIN categories c ON p.category_id = c.category_id;

-- Aggregation functions
SELECT 
  category_id,
  COUNT(*) AS product_count,
  AVG(price) AS average_price,
  MIN(price) AS lowest_price,
  MAX(price) AS highest_price
FROM products
GROUP BY category_id
HAVING COUNT(*) > 5;

-- Subqueries
SELECT product_name, price
FROM products
WHERE price > (
  SELECT AVG(price) 
  FROM products
);

3. Main Uses

SQL is the foundation for data management in many applications:

  • Database administration and management
  • Data retrieval for applications and websites
  • Business intelligence and reporting
  • Data analysis and analytics
  • Maintaining data integrity across systems
  • Data warehousing and data lakes
  • Application back-end development
  • Data migration and integration

4. Pros and Cons

Advantages

  • Standardized language across many database systems
  • Declarative nature makes it readable and approachable
  • Powerful querying capabilities with JOINs and subqueries
  • ACID compliance ensures data integrity
  • Well-established with decades of optimization
  • Excellent for structured, relational data
  • Wide industry adoption and job market demand

Limitations

  • Not ideal for unstructured or highly hierarchical data
  • Can be verbose for complex queries
  • Different database vendors implement SQL slightly differently
  • Limited procedural capabilities without extensions
  • Performance challenges with very large datasets
  • Not designed for distributed systems originally
  • Scaling horizontally can be complex

5. SQL Evolution

SQL has evolved significantly over the decades:

  • 1970s - Early development at IBM with System R
  • 1986 - SQL-86 becomes the first standard (ANSI)
  • 1989 - SQL-89 adds integrity constraints
  • 1992 - SQL-92 (SQL2) major revision with many new features
  • 1999 - SQL:1999 (SQL3) adds recursive queries, triggers, OO features
  • 2003 - SQL:2003 adds XML-related features, window functions
  • 2008 - SQL:2008 adds TRUNCATE statement, refined window functions
  • 2011 - SQL:2011 adds temporal databases, pipelined DML
  • 2016 - SQL:2016 adds JSON, polymorphic table functions
  • 2023 - SQL:2023 adds property graph queries, multi-dimensional arrays

Modern SQL implementations have continued to add features like JSON support, window functions, common table expressions (CTEs), and various performance optimizations while maintaining the core declarative nature of the language.

6. Learning Resources

Here are some excellent resources for learning SQL:

7. Related Technologies

Popular SQL implementations and related technologies:

C:\> cd ../