Query Language
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.
-- 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');
-- 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';
-- 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 );
SQL is the foundation for data management in many applications:
SQL has evolved significantly over the decades:
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.
Here are some excellent resources for learning SQL:
Popular SQL implementations and related technologies: