SQL Cheatsheet: The Ultimate Guide That Saved My Time and Effort

Listen to this Post

SQL is a powerful tool for managing and manipulating data. Whether you’re a beginner or an experienced data engineer, mastering SQL commands can significantly enhance your productivity. Below is a detailed guide with practical examples and commands to help you get started or refine your skills.

You Should Know:

Basic Commands

1. Retrieve Data with SELECT

  • The `SELECT` command is fundamental in SQL. It retrieves data from a database.
  • Example: To get the names of all employees:
    SELECT name FROM employees;
    

2. Filter Results with WHERE

  • The `WHERE` clause filters records based on specific conditions.
  • Example: To find all employees in the Sales department:
    SELECT * FROM employees WHERE department = 'Sales';
    

3. Insert Records with INSERT

  • The `INSERT` command adds new records to a table.
  • Example: To add a new employee named John, aged 30:
    INSERT INTO employees (name, age) VALUES ('John', 30);
    

4. Modify Data with UPDATE

  • The `UPDATE` command modifies existing records.
  • Example: To change John’s age to 31:
    UPDATE employees SET age = 31 WHERE name = 'John';
    

5. Remove Records with DELETE

  • The `DELETE` command removes records from a table.
  • Example: To remove John from the employee list:
    DELETE FROM employees WHERE name = 'John';
    

Advanced Commands

1. Combine Data with JOIN

  • The `JOIN` command combines rows from two or more tables based on a related column.
  • Example: To link customer orders with their details:
    SELECT orders.id, customers.name 
    FROM orders 
    INNER JOIN customers ON orders.customer_id = customers.id;
    

2. Group Data with GROUP BY

  • The `GROUP BY` command groups rows that have the same values into summary rows.
  • Example: To count the number of employees in each department:
    SELECT department, COUNT(*) 
    FROM employees 
    GROUP BY department;
    

3. Sort with ORDER BY

  • The `ORDER BY` command sorts the result set in ascending or descending order.
  • Example: To find the highest earners by sorting salaries in descending order:
    SELECT name, salary 
    FROM employees 
    ORDER BY salary DESC;
    

4. Filter Groups with HAVING

  • The `HAVING` clause filters groups based on a condition.
  • Example: To identify departments with more than 5 employees:
    SELECT department, COUNT(<em>) 
    FROM employees 
    GROUP BY department 
    HAVING COUNT(</em>) > 5;
    

5. Use CTEs (Common Table Expressions)

  • CTEs allow you to break complex queries into manageable parts.
  • Example:
    WITH DepartmentCount AS (
    SELECT department, COUNT(*) AS num_employees
    FROM employees
    GROUP BY department
    )
    SELECT * FROM DepartmentCount WHERE num_employees > 5;
    

6. Leverage Window Functions

  • Window functions perform calculations across a set of table rows related to the current row.
  • Example: To calculate the running total of salaries:
    SELECT name, salary, 
    SUM(salary) OVER (ORDER BY salary) AS running_total
    FROM employees;
    

What Undercode Say:

Mastering SQL commands can transform the way you handle data, making your queries more efficient and your workflow smoother. By understanding and applying these commands, you can save time and effort, allowing you to focus on more complex tasks. Whether you’re filtering data with WHERE, combining tables with JOIN, or analyzing data with GROUP BY, these commands are essential tools in your SQL toolkit.

For further reading and advanced techniques, consider exploring resources like SQL Tutorial or W3Schools SQL.

Linux/Windows Commands Related to SQL:

  • Linux:
  • To connect to a MySQL database:
    mysql -u username -p
    
  • To execute a SQL script from the command line:
    mysql -u username -p database_name < script.sql
    

  • Windows:

  • To start MySQL service:
    net start mysql
    
  • To stop MySQL service:
    net stop mysql
    

By integrating these commands into your workflow, you can further enhance your efficiency and productivity in managing databases.

References:

Reported By: Im Nsk – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image