0% found this document useful (0 votes)
22 views10 pages

Cheat Sheet SQL

Uploaded by

Rohan Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Cheat Sheet SQL

Uploaded by

Rohan Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CHEAT SHEET

1. CREATE DATABASE: CREATE A NEW DATABASE


CREATE DATABASE company;
This command creates a new database named "company."

2. USE: SELECT A SPECIFIC DATABASE TO WORK WITH


USE company;
This command selects the database named "company" for
further operations.

3. ALTER DATABASE: MODIFY A DATABASE'S ATTRIBUTES


ALTER DATABASE database_name

4. DROP DATABASE: DELETE AN EXISTING DATABASE


DROP DATABASE company;
This command deletes the database named "company" and
all its associated data.

5. CREATE: CREATE A NEW TABLE, DATABASE OR INDEX


CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
This command creates a table named "employees" with
columns for employee ID, first name, last name,
department, and salary. The employee_id column is set
as the primary key.

6. INSERT INTO: ADD NEW RECORDS TO A TABLE


INSERT INTO employees (employee_id, first_name, last_name,
department, salary)
VALUES
(1, 'John', 'Doe', 'HR', 50000.00),
(2, 'Jane', 'Smith', 'IT', 60000.00),
(3, 'Alice', 'Johnson', 'Finance', 55000.00),
(4, 'Bob', 'Williams', 'IT', 62000.00),
(5, 'Emily', 'Brown', 'HR', 48000.00);
This command inserts sample data into the "employees"
table with values for employee ID, first name, last
name, department, and salary.

7. ALTER TABLE: MODIFY AN EXISTING TABLE'S


STRUCTURE
ALTER TABLE employees
ADD COLUMN new_column INT;
This command adds a new column named "new_column" of
integer type to the existing "employees" table.
8. DROP TABLE: DELETE A TABLE AND ITS DATA
DROP TABLE employees;
This command deletes the entire "employees" table along
with all its data.

9. SELECT: RETRIEVE DATA FROM ONE OR MORE TABLES


SELECT * FROM employees;
This query will retrieve all columns from the employees
table.

10. DISTINCT: SELECT UNIQUE VALUES FROM A COLUMN


SELECT DISTINCT department FROM employees;
This query will return unique department names from the
employees table.

11. WHERE: FILTER ROWS BASED ON SPECIFIED


CONDITIONS
SELECT * FROM employees WHERE salary > 55000.00;
This query will return employees whose salary is greater
than 55000.00.

12. LIMIT: LIMIT THE NUMBER OF ROWS RETURNED IN


THE RESULT SET
SELECT * FROM employees LIMIT 3;
This query will limit the result set to the first 3
rows.

13. OFFSET: SKIP A SPECIFIED NUMBER OF ROWS


BEFORE RETURNING THE RESULT SET
SELECT * FROM employees LIMIT 10000 OFFSET 2;
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
SELECT * FROM employees FETCH FIRST 3 ROWS ONLY;
This query will fetch the first 3 rows from the result
set.

15. CASE: PERFORM CONDITIONAL LOGIC IN A QUERY


SELECT
first_name,
last_name,
CASE
WHEN salary > 55000 THEN 'High'
WHEN salary > 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;
This query will categorize employees based on their
salary into 'High', 'Medium', or 'Low'.

16. UPDATE: MODIFY EXISTING RECORDS IN A TABLE


UPDATE employees
SET salary = 55000.00
WHERE employee_id = 1;
This query will update the salary of the employee with
employee_id 1 to 55000.00.

17. DELETE: REMOVE RECORDS FROM A TABLE


DELETE FROM employees
WHERE employee_id = 5;
This query will delete the record of the employee with
employee_id 5 from the employees table.

18. WHERE: FILTER ROWS BASED ON SPECIFIED


CONDITIONS
SELECT * FROM employees
WHERE department = 'IT';
This query will retrieve all employees who work in the
IT department.

19. LIKE: MATCH A PATTERN IN A COLUMN


SELECT * FROM employees
WHERE first_name LIKE 'J%';
This query will retrieve all employees whose first name
starts with 'J'.

20. IN: MATCH ANY VALUE IN A LIST


SELECT * FROM employees
WHERE department IN ('HR', 'Finance');
This query will retrieve all employees who work in the
HR or Finance departments.

21. BETWEEN: MATCH VALUES WITHIN A SPECIFIED


RANGE
SELECT * FROM employees
WHERE salary BETWEEN 50000 AND 60000;
This query will retrieve all employees whose salary is
between 50000 and 60000.

22. IS NULL: MATCH NULL VALUES


SELECT * FROM employees
WHERE department IS NULL;
This query will retrieve all employees where the
department is not assigned (NULL).

23. ORDER BY: SORT THE RESULT SET


SELECT * FROM employees
ORDER BY salary DESC;
This query will retrieve all employees sorted by salary
in descending order.
.
24. AND: COMBINES MULTIPLE CONDITIONS IN A WHERE
CLAUSE
SELECT * FROM employees
WHERE department = 'IT' AND salary > 60000;
This query will retrieve employees who work in the IT
department and have a salary greater than 60000.

25. OR: SPECIFIES MULTIPLE CONDITIONS WHERE ANY


ONE OF THEM SHOULD BE TRUE
SELECT * FROM employees
WHERE department = 'HR' OR department = 'Finance';
This query will retrieve employees who work in either
the HR or Finance department.

26. NOT: NEGATES A CONDITION


SELECT * FROM employees
WHERE NOT department = 'IT';
This query will retrieve employees who do not work in
the IT department.

27. LIKE: SEARCHES FOR A SPECIFIED PATTERN IN A


COLUMN
SELECT * FROM employees
WHERE first_name LIKE 'J%';
This query will retrieve employees whose first name
starts with 'J'.

28. IN: CHECKS IF A VALUE MATCHES ANY VALUE IN


SELECT * FROM employees
WHERE department IN ('HR', 'Finance');
This query will retrieve employees who work in the HR
or Finance departments.

29. BETWEEN: SELECTS VALUES WITHIN A SPECIFIED


RANGE
SELECT * FROM employees
WHERE salary BETWEEN 50000 AND 60000;
This query will retrieve employees whose salary is
between 50000 and 60000.

30. IS NULL: CHECKS IF A VALUE IS NULL


SELECT * FROM employees
WHERE department IS NULL;
This query will retrieve employees where the department
is not assigned (NULL).
31. ORDER BY: SORTS THE RESULT SET IN ASCENDING
OR DESCENDING ORDER
SELECT * FROM employees
ORDER BY salary DESC;
This query will retrieve all employees sorted by salary
in descending order.

32. GROUP BY: GROUPS ROWS THAT HAVE THE SAME


VALUES INTO SUMMARY ROWS
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query will group employees by department and count
the number of employees in each department.

33. COUNT: COUNT THE NUMBER OF ROWS IN A RESULT


SET
SELECT COUNT(*) FROM employees;
This query will count the total number of employees.

34. SUM: CALCULATE THE SUM OF VALUES IN A COLUMN


SELECT SUM(salary) FROM employees;
This query will calculate the total salary of all
employees.

35. AVG: CALCULATE THE AVERAGE VALUE OF A COLUMN


SELECT AVG(salary) FROM employees;
This query will calculate the average salary of all
employees.

36. MIN: FIND THE MINIMUM VALUE IN A COLUMN


SELECT MIN(salary) FROM employees;
This query will find the minimum salary among all
employees.

37. MAX: FIND THE MAXIMUM VALUE IN A COLUMN


SELECT MAX(salary) FROM employees;
This query will find the maximum salary among all
employees.

38. GROUP BY: GROUP ROWS BASED ON A SPECIFIED


COLUMN
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query will group employees by department and count
the number of employees in each department.
39. HAVING: FILTER GROUPS BASED ON SPECIFIED
CONDITIONS
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 55000;
This query will calculate the average salary for each
department and return only those departments where the
average salary is greater than 55000.

40. PRIMARY KEY: UNIQUELY IDENTIFIES EACH RECORD


IN A TABLE
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
employee_id is designated as the primary key, ensuring
that each employee record has a unique identifier.

41. FOREIGN KEY: ESTABLISHES A RELATIONSHIP


BETWEEN TWO TABLES
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES
departments(department_id)
);
department_id column in the employees table is a foreign
key that references the department_id column in the
departments table, establishing a relationship between
the two tables.

42. UNIQUE: ENSURES THAT ALL VALUES IN A COLUMN


ARE UNIQUE
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE
);
email column must contain unique values for each
employee.

43. NOT NULL: ENSURES THAT A COLUMN DOES NOT


CONTAIN NULL VALUES
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL
);
first_name and last_name columns must have values and
cannot be NULL.

44. CHECK: SPECIFIES A CONDITION THAT MUST BE MET


FOR A COLUMN'S VALUE
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
age column must have a value of 18 or greater due to the
CHECK constraint.
.
45. INNER JOIN: RETRIEVES RECORDS THAT HAVE
MATCHING VALUES IN BOTH TABLES
SELECT * FROM employees
INNER JOIN departments ON employees.department_id =
departments.department_id;
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
SELECT * FROM employees
LEFT JOIN departments ON employees.department_id =
departments.department_id;
This query will retrieve all records from the employees
table and only the matching records from the departments
table.

47. RIGHT JOIN: RETRIEVES ALL RECORDS FROM THE


RIGHT TABLE AND THE MATCHED RECORDS FROM THE LEFT
TABLE
SELECT * FROM employees
RIGHT JOIN departments ON employees.department_id =
departments.department_id;
This query will retrieve all records from the
departments table and only the matching records from the
employees table.

48. FULL OUTER JOIN: RETRIEVES ALL RECORDS WHEN


THERE IS A MATCH IN EITHER THE LEFT OR RIGHT
TABLE
SELECT * FROM employees
FULL OUTER JOIN departments ON employees.department_id =
departments.department_id;
This query will retrieve all records from both the
employees and departments tables, including unmatched
records.
49. CROSS JOIN: RETRIEVES THE CARTESIAN PRODUCT
OF THE TWO TABLES
SELECT * FROM employees
CROSS JOIN departments;
This query will retrieve all possible combinations of
records from the employees and departments tables.

50. SELF JOIN: JOINS A TABLE TO ITSELF


SELECT e1.first_name, e2.first_name
FROM employees e1, employees e2
WHERE e1.employee_id = e2.manager_id;
In this example, the employees table is joined to itself
to find employees and their respective managers based
on the manager_id column.

51. SCALAR FUNCTIONS: FUNCTIONS THAT RETURN A


SINGLE VALUE
SELECT UPPER(first_name) AS upper_case_name FROM employees;
This query uses the UPPER() scalar function to convert
the first_name column values to uppercase.

52. AGGREGATE FUNCTIONS: FUNCTIONS THAT OPERATE


ON A SET OF VALUES AND RETURN A SINGLE VALUE
SELECT AVG(salary) AS average_salary FROM employees;
This query uses the AVG() aggregate function to
calculate the average salary of all employees.

53. STRING FUNCTIONS: FUNCTIONS THAT MANIPULATE


STRING VALUES
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM
employees;
This query uses the CONCAT() string function to
concatenate the first_name and last_name columns into a
single column called full_name.
SELECT SUBSTR(first_name, 1, 3) AS short_name FROM employees;
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;
This query first concatenates the first_name and
last_name columns into a single column called
full_name. Then, it uses the INSERT() function to
insert the string 'Amazing ' at the 6th position of
the full_name column for each employee. The modified
names are displayed in a new column called
modified_name.
54. DATE AND TIME FUNCTIONS: FUNCTIONS THAT
OPERATE ON DATE AND TIME VALUES
SELECT CURRENT_DATE AS current_date FROM dual;
This query uses the CURRENT_DATE date function to
retrieve the current date.

55. MATHEMATICAL FUNCTIONS: FUNCTIONS THAT


PERFORM MATHEMATICAL OPERATIONS
SELECT SQRT(25) AS square_root FROM dual;
This query uses the SQRT() mathematical function to
calculate the square root of 25.
.
56. SINGLE-ROW SUBQUERY: RETURNS ONE ROW OF
RESULT
SELECT first_name, last_name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
In this example,
the subquery (SELECT MAX(salary) FROM employees) returns
a single row containing the maximum salary, and it's
used to filter employees who have the maximum salary.

57. MULTIPLE-ROW SUBQUERY: RETURNS MULTIPLE ROWS


OF RESULT
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.

58. CORRELATED SUBQUERY: REFERENCES A COLUMN FROM


THE OUTER QUERY
SELECT first_name, last_name
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE
department = e.department);
In this example, the subquery (SELECT AVG(salary) FROM
employees WHERE department = e.department) is correlated
with the outer query by referencing the department
column from the outer query. It calculates the average
salary for each department and is used to filter
employees whose salary is greater than the average
salary of their respective department.

59. NESTED SUBQUERY: A SUBQUERY INSIDE ANOTHER


SUBQUERY
SELECT first_name, last_name
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE department_name = 'IT'
);

You might also like