SQL Database Cheat Sheet-1
SQL Database Cheat Sheet-1
Specify data types, structures and constraints for the data to be stored in the database.
• CREATE:
• DROP:
• ALTER:
1
• Data Types:
• SQL Constraints:
NOT NULL: A Constraint that ensures that a column cannot have NULL value.
DEFAULT: A Constraint that provides a default value for a column when none is specified.
UNIQUE: A Constraint that ensures that all values in a column are different or NULL.
PRIMARY KEY (PK): A Constraint that uniquely identify each row/record in a database table
(NOT NULL + UNIQUE).
FOREIGN KEY (FK): A Constraint that ensures referential integrity. A foreign key from 1
table to another is used link a tuple in the 1st table to a unique tuple in the 2nd table.
CHECK: A constraint that ensures that all values in a column satisfy a certain condition.
2
DML ( Data Manipulation Language ):
• INSERT:
• UPDATE:
• DELETE:
// OR
DELETE FROM <Table_Name> WHERE Id IN ( 10, 20 );
3
• SELECT:
// using LIKE
SELECT Name FROM Student WHERE Name LIKE ‘A%’;
// Ordering values
SELECT Name, Age FROM Student ORDER BY Age;
SELECT Name, Age FROM Student ORDER BY Age DESC;
// SQL Alias
SELECT Products.productid FROM Products AS ID
4
• JOINS:
// Cross Join = It gives combinations of each row of first table with all records
in second table.
SELECT column_name(s)
FROM table1 CROSS JOIN table2;
CROSS JOIN
5
• SET OPERATORS: ( The columns: same order, data type, and number )
// UNION
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2
// UNION ALL
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2
// INTERSECT
SELECT column_name(s) FROM table1
INTERSECT
SELECT column_name(s) FROM table2
// EXCEPT
SELECT column_name(s) FROM table1
EXCEPT
SELECT column_name(s) FROM table2
• AGGREGATE FUNCTIONS:
( COUNT( ) – SUM( ) – AVR( ) – MAX( ) – MIN( ) )
All aggregate functions ignore NULL values when it performs the calculation, except for
the count function.
COUNT(column_name) → ignore NULL
COUNT(*) → counts NULL
6
• GROUP BY:
Group by is used to group a set of rows by a specific column or columns and use
aggregations over these groups.
• HAVING:
The HAVING clause works as a condition on the groups resulting from the GROUP BY
clause, because the WHERE keyword could not be used with aggregate functions.
// List the number of customers in each country. Only include countries with
more than 5 customers.
SELECT Country, COUNT(CustomerID)
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
7
• SUB QUERY / NESTED QUERY:
A subquery is a query nested within another query.
A subquery is called an inner query while the query that contains the subquery is called
an outer query. A subquery must be enclosed in parentheses.
// Display the first name of all employees who are living in UK and have the
same job title as Omar or Amr.
SELECT FirstName
FROM Employees
WHERE Country = ‘UK’
AND Title IN ( SELECT Title
FROM Employees
WHERE FirstName IN (‘Omar’, ‘Amr’) );
Simple sub queries: inner queries that do not reference any columns in the outer query.
Correlated sub queries: inner queries that do reference columns in the outer query.
// Get the names of customers who placed orders with amount over 5000.
SELECT CustomerName
FROM Customer
WHERE EXISTS ( SELECT Id
FROM CustomerOrder
WHERE CustomerId = Customer.Id
AND TotalAmount > 5000 );
// Get the names of customers who did not place orders with amount over 5000.
SELECT CustomerName
FROM Customer
WHERE NOT EXISTS ( SELECT Id
FROM CustomerOrder
WHERE CustomerId = Customer.Id
AND TotalAmount > 5000 );