SQL Cheat Sheet - Scaler Topics
SQL Cheat Sheet - Scaler Topics
SQL
CHEAT SHEET
SELECT distinct(name)
WHERE condition
DATA Related Commands
CREATE TABLE
Examples:
SQL command used to create a new Create a table called “customers” with
table in a database. columns for
Syntax: customer_id,
customer_name,
CREATE TABLE table_name contact_name, and
( an integer field called age:
column_name1 data_type, CREATE TABLE
column_name2 data_type, customers (
column_name3 data_type, customer_id INT PRIMARY KEY,
... customer_name VARCHAR(50),
) contact_name VARCHAR(50),
age INT
);
INSERT INFO
Examples:
SQL command used to create a new
table in a database. Insert a new row in the customer table
created above.
01 Cheatsheet
ALTER TABLE
Examples:
SQL command that is used to modify the
structure of a table, such as adding or Change the data type of the "age" column
deleting columns. in your "customers" table from INT to BIGINT
Syntax:
ALTER TABLE customers MODIFY age BIGINT;
ALTER TABLE table_name
ADD column_name datatype
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
OR,
ALTER TABLE table_name
DROP COLUMN column_name
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Syntax:
DROP TABLE table_name;
DELETE Command
Examples:
DELETE Command is used to entire the
contents of the table only, not the table DELETE FROM customers;
Syntax:
DELETE FROM table_name;
02 Cheatsheet
Basics
Select Clause
Syntax:
It is a SQL clause that specifies which SELECT column1, column2, ...
columns to retrieve from a database table. FROM table_name;
Examples:
SELECT SELECT
* CourseID,
FROM dept_name
Course; FROM
Course;
03 Cheatsheet
From Clause
Syntax:
It is a SQL clause that specifies the tables SELECT column1, column2, ...
from which to retrieve data for a query. FROM table_name;
Examples:
SELECT
*
FROM
Course;
Where Clause
Syntax:
The "WHERE" clause in SQL is used to filter SELECT column1, column2, ...
data from a table based on a specified set FROM table_name
of conditions. WHERE condition;
Examples:
SELECT CourseID title dept_name credits
* 201 DSA CSE 5
FROM 655 CNS CSE 3
Course 722 TBS CSE 4
WHERE
dept_name = 'CSE';
04 Cheatsheet
ORDER BY
Syntax:
ORDER BY is used to sort the result set by SELECT column_name(s)
one or more columns, in ascending or FROM table_name
descending order. ORDER BY column_name [ASC|DESC]
Examples:
SELECT
*
FROM
Course
ORDER BY
credits DESC;
05 Cheatsheet
Distinct
Syntax:
SELECT DISTINCT
DISTINCT is used in a SELECT statement to (dept_name)
return only unique values from a column. FROM
Course;
Examples:
dept_name
SELECT DISTINCT
CSE
(dept_name)
ECE
FROM ME
Course; SIGCOM
EE
CY
CourseID title dept_name credits
ME
201 DSA CSE 5 IT
202 ASE ECE 3 MS
211 FD ME 3 PH
213 DSP SIGCOM 3 CH
222 BCS EE 4 Choose the BIO
301 RC CY 4 dept_name column CY
303 MET ME 3 CSE
323 SE IT 4 BIO
506 DM MS 4 VLSI
511 OPT PH 3 SIGCOM
518 ORG CH 4 CSE
523 NEO BIO 4
604 WLD CY 3
655 CNS CSE 3
702 MIB BIO 4
710 ECM VLSI 3 distinct(dept_name)
716 SPT SIGCOM 4 BIO
722 TBS CSE 4 CH
CSE
The Table CY
ECE
EE
Choose the IT
distinct values only ME
MS
PH
SIGCOM
VLSI
Output
06 Cheatsheet
Aliases
Syntax:
SELECT DISTINCT
In SQL, aliases are used to give a table, or a (dept_name) AS distinct_dept_name
column in a table, a temporary name. FROM
Course;
Examples:
SELECT DISTINCT
(dept_name) AS distinct_dept_name
FROM
Course;
distinct_dept_name
BIO
CH
CSE
CY
ECE
EE
IT
ME
MS
PH
SIGCOM
VLSI
07 Cheatsheet
Operators
08 Cheatsheet
LIKE Operator
Syntax:
Pattern:
%: Matches any string of zero or more characters.
_: Matches any single character.
Examples:
SELECT SELECT
CourseID, CourseID,
title title
FROM FROM
Course Course
WHERE WHERE
title LIKE "D%"; title LIKE "%D";
SELECT
CourseID,
dept_name
FROM
Course
WHERE
dept_name LIKE "%GCO%";
CouseID title
213 SIGCOM
716 SIGCOM
There are other various operators in SQL such as : Logical Operators: AND, OR, NOT , Mathematical Operators: +, -, *, /, %,
String Operators: || (Concatenation), Comparison Operators: =, <>, <, >, <=, >=
09 Cheatsheet
Grouping Clauses
GROUP BY Clause
Syntax:
Examples:
SELECT
dept_name,
SUM(credits)
FROM
Course
GROUP BY
dept_name;
5 + 3 + 4 = 12
10 Cheatsheet
HAVING Clause
Syntax:
SELECT column_name(s)
The HAVING clause is used to filter groups FROM table_name
based on a condition that aggregates WHERE condition
cannot handle. GROUP BY column_name(s)
HAVING condition;
Examples:
SELECT
dept_name,
SUM(credits)
FROM
Course OUTPUT
GROUP BY
dept_name dept_name Sum(credits)
HAVING BIO 8
SUM(credits) > 5; CH 4
CSE 12
CY 7
CourseID title dept_name credits
ECE 3
201 DSA CSE 5
EE 4
202 ASE ECE 3
IT 4
211 FD ME 3
Do the same with ME 6
213 DSP SIGCOM 3
all distinct values MS 4
222 BCS EE 4
of dept_name PH 3
301 RC CY 4
SIGCOM 7
303 MET ME 3
VLSI 3
323 SE IT 4
506 DM MS 4
511 OPT PH 3
518 ORG CH 4 OUTPUT
Apply the
523 NEO BIO 4
Filter Condition
604 WLD CY 3 dept_name Sum(credits)
655 CNS CSE 3 BIO 8
702 MIB BIO 4 CSE 12
710 ECM VLSI 3 CY 7
716 SPT SIGCOM 4 ME 6
722 TBS CSE 4 SIGCOM 7
5 + 3 + 4 = 12
11 Cheatsheet
Aggregate Functions
Count
Examples:
The COUNT() function returns the number
SELECT SELECT
of rows that match a specified condition
COUNT(*) COUNT(DISTINCT dept_name)
or expression.
FROM FROM
Course; Course;
Syntax:
COUNT(*) COUNT(Distinct dept_name)
COUNT([Distinct] Expression) 18 12
Sum
Examples:
SELECT
The SUM() function in SQL returns the sum
SUM(credits) AS total_cse_credits
of all the values in a selected column.
FROM
Course
WHERE
Syntax: dept_name = 'CSE';
SUM(expression) total_cse_credits
12
Max
Examples:
The MAX() function in SQL returns the SELECT
maximum value of a selected column. MAX(credits) AS maximum_credits
FROM
Course
WHERE
Syntax:
dept_name = 'CSE';
MAX(expression)
maximum_credits
12 Cheatsheet
Min
Examples:
The MIN() function in SQL returns the
SELECT
minimum value of a selected column.
MIN(credits) AS minimum_credits
FROM
Course
Syntax: WHERE
dept_name = 'CSE';
MIN(expression)
minimum_credits
Avg
Examples:
The AVG() function in SQL returns the SELECT
average value of a selected column. AVG(credits)
FROM
Course
WHERE
Syntax: dept_name = 'CSE';
AVG(expression)
Avg(credits)
4.0000
13 Cheatsheet
Join in SQL
INNER JOIN
Syntax:
LEFT JOIN
Syntax:
LEFT JOIN returns all rows from the
left table with corresponding rows SELECT column_name(s)
from the right table. FROM table_name1
If there's no matching row, NULLs LEFT JOIN table_name2
are returned as values from the ON table_name1.column_name=table_name2.column_name;
second table.
RIGHT JOIN
Syntax:
RIGHT JOIN returns all rows from
the right table with corresponding SELECT column_name(s)
rows from the left table. FROM table_name1
If there's no matching row, NULLs RIGHT JOIN table_name2
are returned as values from the ON table_name1.column_name=table_name2.column_name;
left table.
FULL JOIN
Syntax:
FULL JOIN (or explicitly FULL
OUTER JOIN) returns all rows SELECT column_name(s)
FROM table_name1
from both tables.
FULL JOIN table_name2
If there's no matching row in the ON table_name1.column_name=table_name2.column_name;
second table, NULLs are returned.
14 Cheatsheet
SELF JOIN
Syntax:
SELECT column_name(s)
A self join is a regular join, but the
FROM table_name1
table is joined with itself. SELF JOIN table_name1;
A self join is used to join a table
OR
to itself.
SELECT column(s)
FROM table_name1, table_name1;
CROSS JOIN
Syntax:
SELECT column(s)
CROSS JOIN returns all possible FROM table_name1
combinations of rows from both CROSS JOIN table_name2;
tables. OR
There are two syntaxes available. SELECT column(s)
FROM table_name1, table_name2;
1 1
2 2
3 3
Table 1 Table 2
15 Cheatsheet