SQL NOTES
SQL is a standard language for accessing and manipulating databases.
SQL stands for structured query language
Tables are broken up into:
Columns- aka fields.
Rows-aka records.
Semicolons separate each SQL statement
I’LL STICK TO LEARNING SQL FOR SQL SERVER AND MS ACCESS!!!!
SELECT, UPDATE, DELTE, INSERT INTO, CREATE DATABASE, ALTER
DATABASE, CREATE INDEX, DROP INDEX- SQL commands
SELECT
Used to select data from a database
SYNTAX:
SELECT column FROM Table;
To SELECT all, use:
SELECT * FROM Table;
SELECT DISTINCT
Used to select distinctly different data from a database
SYNTAX FOR 2 COLUMNS:
SELECT DISTINCT column, column FROM table;
COUNT DISTINCT
Using distinct with a count function returns the number of different values
in a field.
SELECT COUNT (DISTINCT Column) FROM Table;
In MS Access:
SELECT Count (*) AS newcolumn
FROM (SELECT DISTINCT column FROM Table);
WHERE
Used to filter records that fulfill a specific condition
SYNTAX:
SELECT * FROM table
WHERE condition;
‘Where’ can also be used in UPDATE and DELETE.
NOTE!!
Single quotes are used for text values
No quotes used for numerical values
You can use operators in conditions:
= > < >= <= <> BETWE LIKE IN
EN
ORDER By
Used to sort the result in either ascending or descending order or
alphabetical order.
SYNTAX:
SELECT * FROM Table
ORDER BY Column (DESC optional);
When using both ASC and DESC
SELECT * FROM table
ORDER BY column ASC, Column DESDC;
AND
The ‘WHERE’ clause can contain ‘AND’ operator.
It is used to filter records based on one or more condition
AND- Will only display records if all conditions are met
OR- will display a record if any of the conditions are met.
SELECT * FROM Table
WHERE condition
AND condition
AND (condition OR condition);
Parenthesis is used to get the correct result
When conditions include a word starting with eg.R use: ‘R%’
NOT
Used to produce the result in a negative form.
SYNTAX:
SELECT column
FROM Table
WHERE NOT Condition;
Can also be used with:
NOT LIKE
NOT BETWEEN
NOT IN
INSERT INTO
Used to insert a new record in the table
SYNTAX 1: Specific column and values
INSERT INTO Table (Column1, column2)
VALUES (‘Value1’, ‘Value2’);
SYNTAX 2: values for all columns
INSERT INTO Table
VALUES (‘Value1’, ‘Value2’, ‘Value3’);
Multiple insertion of rows.
INSERT INTO Table (Column1, column2)
VALUES (‘Value1’, ‘Value2’), (‘Value3’, ‘Value4’);
NULL
Used for a field with no value
Used in the WHERE clause
You can’t use Comparison operators to determine if a field or record is
null.
SYNTAX:
SELECT column
FROM table
WHERE condition IS NULL; OR WHERE condition IS NOT NULL;
UPDATE
Used to modify the existing records in a table
SYNTAX:
UPDATE Table
SET column = ‘value1’, column2 = value2
WHERE condition;
Without the WHERE clause, all records will be updated!!!
Multiple Records:
UPDATE Table
SET Updated condition
WHERE condition;
DELETE
Used to delete existing records in a table
SYNTAX:
DELETE FROM table
WHERE condition;
Without the WHERE clause, all records will be deleted!!!
Delete all records:
DELETE FROM table;
Delete a Table:
DROP TABLE tableName;
TOP, LIMIT, FETCH FIRST, ROWNUM
TOP:
Used to specify the number of records to be returned
SYNTAX:
SELECT TOP NumOfRecords * FROM Customers;
MySQL uses LIMIT
ORACLE uses FETCH FIRST n ROWS ONLY and ROWNUM
Add a WHERE clause:
SELECT TOP n * FROM table
WHERE condition;
Add an ORDER BY clause:
SELECT TOP n * FROM table
ORDER BY column ASC/DESC;
AGGREGATE FUNCTION
Used to perform a calculation on a set of values and return a single value
Often used with the GROUP BY clause of the SELECT statement
GROUP BY splits the result-set into groups of values and the AGGREGATE
FUNCTION can return a single value for each group.
Aggregates- MIN, MAX, COUNT, SUM, AVG
Except COUNT, aggregate functions ignore NULL values.
MIN and MAX
MIN returns the smallest value of a column
MAX returns the largest value of a column
SYNTAX:
SELECT MIN(column)
FROM Table
WHERE condition;
SELECT MAX(column)
FROM Table
WHERE condition;
Set a column name aka alias:
SELECT MIN(column) AS NewName
FROM Table
WHERE condition;
Using Min with GROUP BY:
SELECT MIN(column1) AS NewName, Column2
FROM Table
GROUP BY Column2;
COUNT