SQL Cheat Sheet:: - by Yash Shirodkar
SQL Cheat Sheet:: - by Yash Shirodkar
SQL Cheat Sheet:: - by Yash Shirodkar
- By Yash Shirodkar.
I was able to create this cheet sheat with the help of W3 School and other online available
resources. (Link- https://www.w3schools.com/sql/)
Create Database-
CREATE DATABASE databasename;
Drop Database-
DROP DATABASE databasename;
Backup Database-
BACKUP DATABASE databasename
TO DISK = 'filepath';
Create table-
CREATE TABLE Persons (
PersonID int NOT NULL UNIQUE,
Age int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY Firstname,
Check (age>=21 and city = ‘NY’)
);
Drop Table-
DROP TABLE table_name;
Alter Table-
The ALTER TABLE statement is used to add, drop, or alter columns in an existing
table.
1. ALTER TABLE Customers
ADD Email varchar(255);
2. ALTER TABLE Customers
DROP COLUMN Email;
3. ALTER TABLE table_name
ALTER COLUMN column_name datatype; // to change the datatype of the
table
SQL Constraints-
Check-
The CHECK constraint is used to limit the value range that can be placed in a
column.
SQL Default-
Used to set a default value for a column
E.g.-
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘New York’
);
The DEFAULT constraint can also be used to insert system values, by using
functions like GETDATE():
E.g. -
CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE()
);
Index-
Duplicate values allowed
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Select Query-
Operators-
=, >, <, >=, <=, <>
BETWEEN- for range: use ‘between’
SELECT * FROM Products
WHERE Price BETWEEN 50 AND 60;
LIKE-
SELECT * FROM Customers
WHERE City LIKE 's%';
INSERT-
INSERT INTO table_name
VALUES (value1, value2, value3, ...); //MAINTAIN ORDER OF COLUMNS
OR
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
NULL-
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE-
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE-
DELETE FROM table_name WHERE condition;
LIMIT, TOP-
SELECT * FROM Customers LIMIT 3;
AGGREGATIONS –
COUNT(), SUM(), AVG()
SELECT COUNT(ProductID) AS COUNT
FROM Products;
ALIASES-
Column alias
SELECT column_name AS alias_name
FROM table_name;
Table Alias
SELECT column_name(s)
FROM table_name AS alias_name;
Advanced SQL Concepts:
JOINS-
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
UNION-
The UNION operator is used to combine the result-set of two or
more SELECT statements.
• Every SELECT statement within UNION must have the same number of
columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
HAVING-
The HAVING clause was added to SQL because the WHERE keyword cannot be used
with aggregate functions.
CASE-
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
The following SQL goes through conditions and returns a value when the first
condition is met:
The following SQL will order the customers by City. However, if City is NULL,
then order by Country:
SQL Comments-
-- is used comment sql queries.
HAVING Clause:
It is used to apply condition on aggregation, where is used to apply condition on a column.
SELECT (DISTINCT)
FROM
WHERE
GROUP BY
ORDER BY
HAVING
WINDOW FUNCTIONS-
JOINS, UNION, HAVING, GROUP BY, CASE statements seem easy while looking
at independent examples. But the complexity increases when these statements
are used in combination with each other.
DATE MANIPULATION:
select ifnull(customer_name,'N/A')customer_name,\