sql_cheat_sheet (1)
sql_cheat_sheet (1)
USE company;
This command deletes the database named "company" and all its
associated data.
This query will retrieve all columns from the employees table.
This query will limit the result set to the first 3 rows.
This query retrieves all rows from the "employees" table, skipping
the first 2 rows and limiting the result to 10,000 rows.
14. FETCH: Retrieve A Specified Number Of Rows From
The Result Set
This query will fetch the first 3 rows from the result set.
SELECT
first_name,
last_name,
CASE
WHEN salary > 55000 THEN 'High'
WHEN salary > 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;
UPDATE employees
SET salary = 55000.00
WHERE employee_id = 1;
This query will retrieve all employees whose first name starts with
'J'.
This query will retrieve all employees where the department is not
assigned (NULL).
SQL Operator
learn how to use AND, OR, NOT and others oprtators.
24. AND: Combines Multiple Conditions In A WHERE
Clause
This query will retrieve employees whose first name starts with 'J'.
This query will find the minimum salary among all employees.
This query will find the maximum salary among all employees.
Constraints in SQL
Joins in SQL
Explore different join types to seamlessly merge data from
multiple tables in your SQL queries.
This query will retrieve records from both the employees and
departments tables where there is a match on the department_id
column.
46. LEFT JOIN: Retrieves All Records from the Left
Table and the Matched Records from the Right Table
This query will retrieve all records from the employees table and
only the matching records from the departments table.
This query will retrieve all records from the departments table
and only the matching records from the employees table.
This query will retrieve all records from both the employees and
departments tables, including unmatched records.
SQL Functions
SQL cheat sheet for SQL functions. It is used for common tasks
like aggregation, filtering, date/time manipulation, and more!
This query uses the SUBSTR() function to extract the first three
characters of the first_name column for each employee. The
result is displayed in a new column called short_name.
SELECT INSERT(full_name, 6, 0, 'Amazing ') AS modified_name
FROM (SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees) AS employee_names;
Subqueries in SQL
This SQL cheat sheet explains how to nest queries for powerful
data filtering and manipulation within a single statement.
SELECT department_name
FROM departments
WHERE department_id IN (SELECT department_id FROM employees);
In this example, the subquery (SELECT department_id FROM
employees) returns multiple rows containing department IDs, and
it's used to filter department names based on those IDs.
Transactions in SQL
Learn how to manage groups of database operations as a single
unit for reliable data updates.
BEGIN TRANSACTION;
COMMIT;
ROLLBACK;
WITH high_paid_employees AS (
SELECT * FROM employees WHERE salary > 60000
)
SELECT * FROM high_paid_employees;