0% found this document useful (0 votes)
324 views17 pages

SQL Cheat Sheet - Scaler Topics

The document provides a cheat sheet on common SQL commands and examples for creating tables, inserting data, modifying tables, deleting data, and retrieving data through select statements with clauses like select, from, where, order by, and distinct. Key SQL commands covered include create table, insert, alter table, drop table, delete, and select.

Uploaded by

suriyabe2000
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)
324 views17 pages

SQL Cheat Sheet - Scaler Topics

The document provides a cheat sheet on common SQL commands and examples for creating tables, inserting data, modifying tables, deleting data, and retrieving data through select statements with clauses like select, from, where, order by, and distinct. Key SQL commands covered include create table, insert, alter table, drop table, delete, and select.

Uploaded by

suriyabe2000
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/ 17

CREATE TABLE table_name

DELETE FROM customers;

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.

Syntax: INSERT INTO customers(customer_id,


customer_name, contact_name, age)
INSERT INTO table_name(column1,
column2, column3, ...) VALUES (1, 'John Smith', 'Jane Doe', 30);
VALUES (value1, value2, value3, ...);

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;

DROP TABLE Command


Examples:
DROP TABLE Command is used to entire
the table itself, along with its contents. DROP TABLE customers;

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;

CourseID title dept_name credits CourseID dept_name


201 DSA CSE 5 201 CSE
202 ASE ECE 3 202 ECE
211 FD ME 3 211 ME
213 DSP SIGCOM 3 213 SIGCOM
222 BCS EE 4 222 EE
301 RC CY 4 301 CY
303 MET ME 3 303 ME
323 SE IT 4 323 IT
506 DM MS 4 506 MS
511 OPT PH 3 511 PH
518 ORG CH 4 518 CH
523 NEO BIO 4 523 BIO
604 WLD CY 3 604 CY
655 CNS CSE 3 655 CSE
702 MIB BIO 4 702 BIO
710 ECM VLSI 3 710 VLSI
716 SPT SIGCOM 4 716 SIGCOM
722 TBS CSE 4 722 CSE

Note: If you want to retrieve all columns, use * as shown above.

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;

CourseID title dept_name credits


201 DSA CSE 5
202 ASE ECE 3
211 FD ME 3
213 DSP SIGCOM 3
222 BCS EE 4
301 RC CY 4
303 MET ME 3
323 SE IT 4
506 DM MS 4
511 OPT PH 3
518 ORG CH 4
523 NEO BIO 4
604 WLD CY 3
655 CNS CSE 3
702 MIB BIO 4
710 ECM VLSI 3
716 SPT SIGCOM 4
722 TBS CSE 4

Note: If you want to retrieve all columns, use * as shown above.

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;

CourseID title dept_name credits


201 DSA CSE 5
722 TBS CSE 4
716 SPT SIGCOM 4
222 BCS EE 4
301 RC CY 4
702 MIB BIO 4
323 SE IT 4
506 DM MS 4
518 ORG CH 4
523 NEO BIO 4
511 OPT PH 3
710 ECM VLSI 3
655 CNS CSE 3
604 WLD CY 3
303 MET ME 3
213 DSP SIGCOM 3
211 FD ME 3
202 ASE ECE 3

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

NOT EQUAL TO BETWEEN Operator

<> or ! = The BETWEEN operator is used in SQL to


check whether a value is within a
specified range of values.
Examples:
SELECT Examples:
*
SELECT
FROM
*
Course FROM
WHERE Course
dept_name <> 'CSE'; WHERE
credits BETWEEN 4 AND 5;
CourseID title dept_name credits
202 ASE ECE 3 CourseID title dept_name credits
211 FD ME 3 201 DSA CSE 5
213 DSP SIGCOM 3 222 BCS EE 4
222 BCS EE 4 301 RC CY 4
301 RC CY 4 323 SE IT 4
303 MET ME 3 506 DM MS 4
323 SE IT 4 518 ORG CH 4
506 DM MS 4 523 NEO BIO 4
511 OPT PH 3 702 MIB BIO 4
518 ORG CH 4 716 SPT SIGCOM 4
523 NEO BIO 4 722 TBS CSE 4
604 WLD CY 3
702 MIB BIO 4
710 ECM VLSI 3
716 SPT SIGCOM 4

08 Cheatsheet
LIKE Operator
Syntax:

The LIKE operator in SQL is used to perform SELECT column_name(s)


FROM table_name
pattern matching on string values.
WHERE column_name LIKE pattern;

Pattern:
%: Matches any string of zero or more characters.
_: Matches any single character.

Examples:

Select titles starting with a D. Select titles ending with a D.

SELECT SELECT
CourseID, CourseID,
title title
FROM FROM
Course Course
WHERE WHERE
title LIKE "D%"; title LIKE "%D";

CouseID title CouseID title


201 DSA 211 FD
213 DSP 604 WLD
506 DM

Select dept_name containing


the substring with a "GCO".

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:

The GROUP BY clause is used to group rows SELECT column_name(s)


FROM table_name
that have the same values.
GROUP BY column_name(s);

Examples:

SELECT
dept_name,
SUM(credits)
FROM
Course
GROUP BY
dept_name;

CourseID title dept_name credits


OUTPUT
201 DSA CSE 5
202 ASE ECE 3
211 FD ME 3 dept_name Sum(credits)
213 DSP SIGCOM 3 BIO 8
222 BCS EE 4 CH 4
Do the same with
301 RC CY 4 CSE 12
all distinct values
303 MET ME 3 CY 7
of dept_name
323 SE IT 4 ECE 3
506 DM MS 4 EE 4
511 OPT PH 3 IT 4
518 ORG CH 4 ME 6
523 NEO BIO 4 MS 4
604 WLD CY 3 PH 3
655 CNS CSE 3 SIGCOM 7
702 MIB BIO 4 VLSI 3
710 ECM VLSI 3
716 SPT SIGCOM 4
722 TBS CSE 4

Same value of dept_name i.e. CSE,


thus group these rows and sum
the values of 'credits' column.

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

Same value of dept_name i.e. CSE,


thus group these rows and sum
the values of 'credits' column.

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:

JOIN (or explicitly INNER JOIN) SELECT column_name(s)


returns rows that have matching FROM table_name1
INNER JOIN table_name2
values in both tables. ON table_name1.column_name=table_name2.column_name;

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;

Self Join Right Join Full Join

Inner Join Cross Join Left Join

1 1

2 2

3 3

Table 1 Table 2

15 Cheatsheet

You might also like