Database Cheat Sheet
Database Cheat Sheet
Developed by IBM in the late 1970’s, Endorsed and adopted by ANSI in 1992, Endorsed as a standard by
the International Organization for Standardization (ISO), It is text oriented, Can be used by
• SQL is not a full programming language, but rather a data sublanguage.
• SQL is comprised of:
data definition language (DDL) - used to define database structures
data manipulation language (DML) - data definition and updating, data retrieval (Queries)
SQL/Persistent Stored Modules (SQL/PSM) - procedural programming capabilities
transaction control language (TCL) - control transaction behavior [See Chapter 6]
Data control language (DLC) - grant and revoke database permissions [See Chapter 6]
• The SQL data definition statements include:
CREATE - to create database objects
ALTER - to modify the structure and/or characteristics of database objects
DROP - to delete database objects
TRUNCATE - to delete table data while keeping the structure SQL for DDL CREATE Statement
• Two of the most useful data definition SQL statements are:
the SQL DROP TABLE statement
the SQL ALTER TABLE statement
• SQL DML is used to query databases and to modify data in the tables.
• There are three possible data modification operations:
INSERT - adding data to a relation
UPDATE - modifying data in a relation
DELETE - deleting data in a relation
• The order of the columns on the INSERT command does not matter as long as the values match the order of the
columns as seen below.
• The SQL DML contains commands for the three possible data modifications operations:
Insert, Modify, Delete
• The example above is a valid statement, but note that if you fail to include the WHERE clause then you will have
just deleted all records in the table.
• The basic framework for the SQL Query statement has three statements as follows:
the SQL SELECT clause - specifies which columns are to be listed in the query results
the SQL FROM clause - specifies which tables are to be used in the query
the SQL WHERE clause - specifies which rows are to be listed in the query results
Operator Meaning
= Is equal to
<> Is NOT Equal to
< Is less than
> Is greater than
<= Is less than OR equal to
>= Is greater than OR equal to
IN Is equal to one of a set of values
NOT IN Is NOT Equal to any of a set of values
BETWEEN Is within a range of numbers (includes the end points)
SQL Comparison Operators 2
Operator Meaning
NOT BETWEEN Is NOT within a range of numbers (includes the end points)
LIKE Matches a set of characters
NOT LIKE Does NOT match a set of characters
IS NULL Is equal to NULL
IS NOT NULL Is NOT equal to NULL
• By default, SQL Server sorts in ascending order. If you need the sort in descending order it would be: ORDER BY
Department DESC;
SQL WHERE Clause Options
• Three options for the SQL WHERE clauses are compound clauses, ranges, wildcards
SQL Logical Operators
Operator Meaning
AND Both arguments are TRUE
OR One of the other or both of the arguments are TRUE
NOT Negates the associated operator
• It is possible to involve the use of SQL built-in functions.
SQL Built-in Aggregate Functions
Functions Meaning
COUNT(*) Count the number of rows in the table
COUNT Count the number of rows in the table where column (Name) IS
({Name}) NOT NULL
SUM Calculate the sum of all values (numeric columns only)
AVG Calculate the average of all values (numeric columns only)
MIN Calculate the minimum value of all values
MAX Calculate the maximum value of all values
• In SQL, you can use the SQL GROUP BY clause to group rows by common values. Because there are 9
departments, we have the employees divided into 9 groups.
• The queries considered so far have involved data from a single table. However, at times, more than one table must
be processed to obtain the desired information. SQL provides two different techniques for querying data from multiple
tables:
the SQL Subquery
the SQL Join
• When selection an employee from the joined table:
SELECT firstName, LastName, ProjectID
FROM EMPLOYEE, ASSIGNMENT
WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.Employee number
AND EMPLOYEE.EmployeeNumber = 1
or
SELECT firstName, LastName, ProjectID
FROM EMPLOYEE as e, ASSIGNMENT as a
WHERE e.EmployeeNumber = a.Employee number
AND e.EmployeeNumber = 1
• The JOIN ON syntax still requires a statement of primary key to foreign key as shown below:
• Subqueries and joins both process multiple tables, but they differ slightly. A subquery can only be used to retrieve
data from the top table, whereas a join can be used to obtain data from any number of tables.
• SQL Inner Join is also referred to as an SQL equijoin (or simply join).
• An Inner Join only displays data from the rows that match based on join conditions:
if a row has a value that does not match the WHERE clause condition, that row will not be included in the
join result
• LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
• FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
/* LEFT JOIN */
SELECT *
FROM Student AS s LEFT JOIN Locker AS c
ON s.LockerFK = c.LockerPK;
/* RIGHT JOIN */
SELECT *
FROM Student AS s RIGHT JOIN Locker AS c
ON s.LockerFK = c.LockerPK;
/* FULL OUTER JOIN */
SELECT *
FROM Student AS s FULL OUTER JOIN Locker AS c
ON s.LockerFK = c.LockerPK;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~