SQL Basic Cheat Sheet

SQL SQL

Posted by admin on 2023-07-17 19:00:39 |

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 858


SQL Basic Cheat Sheet

Certainly! Here is a basic cheat sheet for SQL queries:

SELECT Statement:

  • Retrieve data from a table:
    sql
    SELECT column1, column2 FROM table_name;
  • Retrieve all columns from a table:
    sql
    SELECT * FROM table_name;

WHERE Clause:

  • Filter rows based on a condition:
    sql
    SELECT column1, column2 FROM table_name WHERE condition;
  • Comparison operators: =, <> (not equal to), <, >, <=, >=

Logical Operators:

  • Combine multiple conditions:
    • AND operator:
      sql
      WHERE condition1 AND condition2;
    • OR operator:
      sql
      WHERE condition1 OR condition2;
    • NOT operator:
      sql
      WHERE NOT condition;

ORDER BY Clause:

  • Sort the result set:
    sql
    SELECT column1, column2 FROM table_name ORDER BY column1 ASC/DESC;
    ASC: Ascending order (default), DESC: Descending order

LIMIT Clause:

  • Limit the number of rows returned:
    sql
    SELECT column1, column2 FROM table_name LIMIT number_of_rows;

GROUP BY Clause:

  • Group rows based on a column:
    sql
    SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
    Common aggregate functions: COUNT, SUM, AVG, MIN, MAX

JOIN Clause:

  • Combine rows from multiple tables based on a related column:
    • Inner Join:
      sql
      SELECT column1, column2 FROM table1 JOIN table2 ON table1.column = table2.column;
    • Left Join:
      sql
      SELECT column1, column2 FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
    • Right Join:
      sql
      SELECT column1, column2 FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
    • Full Outer Join:
      sql
      SELECT column1, column2 FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column;

UPDATE Statement:

  • Update values in a table:
    sql
    UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

DELETE Statement:

  • Delete rows from a table:
    sql
    DELETE FROM table_name WHERE condition;

These are just some of the most commonly used SQL query commands and clauses. SQL is a powerful language with many more features and syntax options.

Leave a Comment: