​1. What is SQL

  (1) SQL stands for Structured Query Language

  (2) SQL is an ANSI standard

  (3) RDBMS(Relational Database Managerment System) is the basis for SQL like SQLServer, DB2, Oracle, MySQL, and Access eg.

​2. SQL DML and DDL

  (1) The query and update commands from the DML(Data Manipulation Language) part of SQL:

  • SELECT - extracts data from a database

  • UPDATE - updates data in a database

  • DELETE - deletes data from a database

  • INSERT INTO - inserts new data into a database

  (2) The most important DDL(Data Definition Language) statements in SQL:

  • CREATE DATABASE - creates a new database

  • ALTER DATABASE - modifies a database

  • CREATE TABLE - creates a new table

  • ALTER TABLE - modifies a table

  • DROP TABLE - deletes a table

  • CREATE INDEX - creates an index

  • DROP INDEX - deletes an index

​3. SELECT Syntax and keywords

  (1) SELECT Syntax: SELECT column_name(s) FTOM table_name

  (2) DISTINCT: can be used to return only distinct values

  (3) WHERE: is used to filter records

  • Operators allowed in the WHERE clause: =, <>, >, <, >=, <=, AND, OR, BETWEEN, LIKE, IN

  (4) ORDER BY: is used to sort the result-set

  • Keyworks for ORDER BY: ASC, DESC

​4. INSERT INTO Syntax

  • INSERT INTO table_name VALUES(value1, value2, value3, …) 

  • INSERT INTO table_name(column1, column2, column3, …) VALUES(value1, value2, value3,…)

​5. UPDATE Syntax

  • UPDATE table_name SET column1 = value1, column2 = value2, … WHERE some_column = some_value

​6. DELETE Syntax

  • DELETE FROM table_name WHERE some_column = some_value

​7. SQL Aggregate functions and Syntax

  (1) AVG() - returns the average value

  • SELECT AVG(column_name) FROM table_name

  (2) COUNT() - returns the number of rows

  • SELECT COUNT(*) FROM table_name

  • SELECT COUNT(DISTINCT column_name) FROM table_name

  (3) FIRST() - returns the first value

  • SELECT FIRST(column_name) FROM table_name

  (4) LAST() - returns the last value

  • SELECT LAST(column_name) FROM table_name

  (5) MAX() - returns the largest value

  • SELECT MAX(column_name) FROM table_name

  (6) MIN() - returns the smallest value

  • SELECT MIN(column_name) FROM table_name

  (7) SUM() - return the sum

  • SELECT SUM(column_name) FROM table_name

  (8) GROUP BY - is used in conjunction with the aggregate functions to group the result-set by one or more columns

  • SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name

  (9) HAVING - was added to SQL because the WHERE keyword could not be used with aggregate functions

  • SELECT column_name, agregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value

​8. SQL Scalar functions

  (1) UCASE() - converts a field to upper case

  • SELECT UCASE(column_name) FROM table_name

  (2) LCASE() - converts a field to lower case

  • SELECT LCASE(column_name) FROM table_name

  (3) MID() - extract characters from a text field

  • SELECT MID(column_name, start[, length]) FROM table_name

  (4) LEN() - returns the length of a text field

  • SELECT LEN(column_name) FROM table_name

  (5) ROUND() - rounds a numeric field to the number of decimals specified

  • SELECT ROUND(column_name, decimals) FROM table_name

  (6) NOW() - returns the current system date and time

  • SELECT NOW() FROM table_name

  (7) FORMAT() - formats how a field is to be diaplayed

  • SELECT FORMAT(column_name, format) FROM table_name

  (8) TOP - is used to specify the number of records to return

  • SELECT TOP number|percent column_name(s) FROM table_name

  (9) LIKE - is used to search for a specified pattern in a column

  • SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern

  (10) IN - allows you to specify multiple values in a WHERE clause

  • SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, …)

  (11) BETWEEN AND - selects a range of data between two values

  • SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2

​9. SQL Wildcard

  • % - a substitute for zero or more characters

  • _ - a substitute for exactly one character

  • [charlist] - any single charater in charlist

  • [\^charlist] or [!charlist] - any single character not in charlist

​10. SQL Alias Syntax

  • SELECT column_name(s) FROM table_name AS alias_name

  • SELECT column_name AS alias_name FROM table_name

​11. SQL Joins

  • SQL joins are used to query data from two or more tables, base on a relationship between certain columns in these tablse

  • Different SQL JOINS:

  (1) JOIN or INNER JOIN - return rows when there is at least one match in both tables

  • SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name = table_name2.column_name

  (2) LEFT JOIN - return all rows from the left table, even if there are no matches in the right table

  • SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name = table_name2.column_name

  (3) RIGHT JOIN - return all rows from the right table, even if there are no matches in the left table

  • SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name = table_name2.column_name

  (4) FULL JOIN - return rows when there is a match in one of the tables

  • SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name = table_name2.column_name

​12. SQL UNION Operator

  (1) UNION operator is used to combine the result-set of two or more SELECT statements

  (2) Syntax

  • SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2

  • SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2

​13. SQL SELECT INTO Statement

  (1) SELECT INTO statement can be used to create backup copies of tables

  (2) Syntax:

  • SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename

  • SELECT column_name(s) INTO new_table_name [IN externaldatabase] FROM old_tablename