πŸ“ SQL Cheat Sheet for Developers

nullvoidkage

Nikko Ferwelo

Posted on August 22, 2024

πŸ“ SQL Cheat Sheet for Developers

Hey everyone! πŸ‘‹

I’ve compiled a handy SQL Cheat Sheet to help you quickly reference key SQL commands and concepts. Whether you’re working with databases or just need a quick refresher, this guide has got you covered.

Let’s dive in!


πŸ”— Core Concepts

  • SQL: Structured Query Language for managing and manipulating relational databases.

πŸ“¦ SQL Commands and Concepts

  • SELECT: Retrieves data from tables.
  SELECT * FROM employees;
Enter fullscreen mode Exit fullscreen mode
  • INSERT: Adds new records to tables.
  INSERT INTO employees (name, position) VALUES ('John Doe', 'Developer');
Enter fullscreen mode Exit fullscreen mode
  • UPDATE: Modifies existing records in tables.
  UPDATE employees SET position = 'Senior Developer' WHERE name = 'John Doe';
Enter fullscreen mode Exit fullscreen mode
  • DELETE: Removes records from tables.
  DELETE FROM employees WHERE name = 'John Doe';
Enter fullscreen mode Exit fullscreen mode
  • WHERE: Filters records based on conditions.
  SELECT * FROM employees WHERE position = 'Developer';
Enter fullscreen mode Exit fullscreen mode
  • JOIN: Combines records from multiple tables.
  SELECT employees.name, departments.department_name
  FROM employees
  JOIN departments ON employees.department_id = departments.id;
Enter fullscreen mode Exit fullscreen mode
  • INNER JOIN: Returns matching records from tables.

    SELECT * FROM orders
    INNER JOIN customers ON orders.customer_id = customers.id;
    
  • LEFT JOIN: Returns all left table records.

    SELECT * FROM employees
    LEFT JOIN departments ON employees.department_id = departments.id;
    
  • RIGHT JOIN: Returns all right table records.

    SELECT * FROM employees
    RIGHT JOIN departments ON employees.department_id = departments.id;
    
  • FULL JOIN: Returns all matching/non-matching records.

    SELECT * FROM employees
    FULL JOIN departments ON employees.department_id = departments.id;
    
    • GROUP BY: Groups rows sharing common fields.
  SELECT department_id, COUNT(*) as employee_count
  FROM employees
  GROUP BY department_id;
Enter fullscreen mode Exit fullscreen mode
  • ORDER BY: Sorts records in ascending/descending order.
  SELECT * FROM employees ORDER BY name ASC;
Enter fullscreen mode Exit fullscreen mode
  • HAVING: Filters groups after aggregation.
  SELECT department_id, COUNT(*) as employee_count
  FROM employees
  GROUP BY department_id
  HAVING COUNT(*) > 5;
Enter fullscreen mode Exit fullscreen mode
  • DISTINCT: Removes duplicate records from results.
  SELECT DISTINCT department_id FROM employees;
Enter fullscreen mode Exit fullscreen mode
  • LIMIT: Restricts the number of returned records.
  SELECT * FROM employees LIMIT 10;
Enter fullscreen mode Exit fullscreen mode
  • OFFSET: Skips a specific number of records.
  SELECT * FROM employees LIMIT 10 OFFSET 20;
Enter fullscreen mode Exit fullscreen mode
  • ALIAS: Renames tables or columns temporarily.
  SELECT name AS employee_name FROM employees;
Enter fullscreen mode Exit fullscreen mode
  • UNION: Combines results of two SELECTs.
  SELECT name FROM employees
  UNION
  SELECT name FROM contractors;
Enter fullscreen mode Exit fullscreen mode
  • INDEX: Speeds up data retrieval.
  CREATE INDEX idx_employee_name ON employees (name);
Enter fullscreen mode Exit fullscreen mode
  • PRIMARY KEY: Uniquely identifies each table record.
  CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100)
  );
Enter fullscreen mode Exit fullscreen mode
  • FOREIGN KEY: Links records between tables.
  CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
  );
Enter fullscreen mode Exit fullscreen mode
  • AUTO_INCREMENT: Automatically increments numeric values.
  CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100)
  );
Enter fullscreen mode Exit fullscreen mode
  • NOT NULL: Ensures column must have a value.
  CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
  );
Enter fullscreen mode Exit fullscreen mode
  • DEFAULT: Sets default value for column.
  CREATE TABLE employees (
    id INT PRIMARY KEY,
    status VARCHAR(20) DEFAULT 'active'
  );
Enter fullscreen mode Exit fullscreen mode
  • CHECK: Ensures column meets a condition.
  CREATE TABLE employees (
    id INT PRIMARY KEY,
    age INT CHECK (age >= 18)
  );
Enter fullscreen mode Exit fullscreen mode
  • CONSTRAINT: Defines rules for table data integrity.
  ALTER TABLE employees
  ADD CONSTRAINT unique_name UNIQUE (name);
Enter fullscreen mode Exit fullscreen mode
  • TRIGGER: Executes automatic actions on data changes.
  CREATE TRIGGER before_employee_insert
  BEFORE INSERT ON employees
  FOR EACH ROW
  BEGIN
    SET NEW.created_at = NOW();
  END;
Enter fullscreen mode Exit fullscreen mode
  • VIEW: Virtual table based on a query.
  CREATE VIEW employee_view AS
  SELECT name, position FROM employees;
Enter fullscreen mode Exit fullscreen mode
  • SUBQUERY: Nested query within another query.
  SELECT name FROM employees
  WHERE department_id IN (
    SELECT id FROM departments WHERE name = 'Sales'
  );
Enter fullscreen mode Exit fullscreen mode
  • TRANSACTION: Ensures data consistency across operations.
  START TRANSACTION;
  UPDATE employees SET position = 'Manager' WHERE name = 'John Doe';
  COMMIT;
Enter fullscreen mode Exit fullscreen mode
  • COMMIT: Saves all changes in a transaction.
  COMMIT;
Enter fullscreen mode Exit fullscreen mode
  • ROLLBACK: Undoes changes in a transaction.
  ROLLBACK;
Enter fullscreen mode Exit fullscreen mode
  • ACID: Ensures reliable database transactions (Atomicity, Consistency, Isolation, Durability).

Connect with me:

Feel free to reach out or follow me for more content on database management and SQL. Happy querying! πŸ’»


πŸ’– πŸ’ͺ πŸ™… 🚩
nullvoidkage
Nikko Ferwelo

Posted on August 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related