SQL Notes
SQL Notes
!SQL stands for Structured Query Language. It's used for creating, querying, updating and
manipulating modern relational databases. SQL is used on virtually all major platforms, and by
virtually all major relational database systems.!
The SQL language can be divided into two major categories. The Data Definition Language, or
DDL, is used to define tables, indexes, and relationships. And the Data Manipulation Language,
or DML, is used to add, query, manipulate, and delete data from tables and datasets.!
How SQL works is it understands fields that are in tables, and how to find the data in the tables
based on the contents of the fields. All SQL operations are then one of four general things to do
with tables:!
Create!
Putting data into tables.!
Read!
Query data out of a table.!
Update!
Change data already in a table.!
Delete!
Remove data from the table.!
This has been given the acronym "CRUD" and is considered a fundamental set of features every
data storage system must have.!
!
One way to explain how SQL works is by comparing it to a spreadsheet software like Excel:!
!
!
!
!
!
!
FUNDAMENTALS
SELECT !
SELECT * FROM Table_Name;!
SELECT Column1, Column2, FROM Table_Name;!
Rename Column1 to Column One
SELECT Column1 AS Column One FROM Table_Name;!
Work with NULL NULL is not empty, false or zero. Its a lack of data.
SELECT * FROM Table_Name WHERE Column1 IS NULL;
SELECT * FROM Table_Name WHERE Column1 IS NOT NULL;
SELECT * FROM Table_Name WHERE Column1 < 0 OR Column1 IS NULL;!
To work with IN operator IN operators use to select results that match values in a list
SELECT * FROM Table_Name WHERE Continent IN (Europe, Asia);!
%, the wildcard in SQL, matches zero or more characters in a string while the _ matches a single character.
INSERT!
INSERT INTO Table_Name ( Column1, Column3, Column9 ) VALUES ( A, 999, ABC);!
To insert a new row without specific field (values MUST be equal to number of columns)
INSERT INTO Table_Name VALUES ( 1, one, integer);!
INSERT INTO Table_Name (Column1, Column2) SELECT id, name FROM Table2;!
CREATE!
CREATE TABLE Table_Name (
id INTEGER PRIMARY KEY, A sequence generator2. It will create sequential
numbers in this column.
aaa INTEGER NOT NULL, A NOT NULL constraint
bbb TEXT DEFAULT panda, A pre-defined value. If bbb is null then panda appears
ccc TEXT UNIQUE Note that NULL is unique. Its possible to have 2 rows with NULL
in column ccc.
) ;!
!
!
ALTER!
To add a new column ddd to an existing table
ALTER TABLE Table_Name ADD ddd TEXT;!
UPDATE!
DELETE!
DROP!
!
!
!
!
!
RELATIONSHIP
Related tables with JOIN
1. Select the intersection of the two tables
The result will include rows from both tables where the joined condition is met.
SELECT l.description AS column1, r.description AS column2
FROM left_table AS l left table
JOIN right_table AS r
ON l.id = r.id the condition
;!
2. Select the left outer join (even the condition is not met)
A left outer join includes the rows where the condition is met,
plus all the rows from the left table, where the condition is not met.
SELECT l.description AS column1, r.description AS column2
FROM left_table AS l
LEFT JOIN right_table AS r
ON l.id = r.id
;!
STRINGS
Finding the length of a string with LENGTH
SELECT Name, LENGTH(Name) AS Length_Column_Name FROM Table_Name;!
!
!
!
NUMBERS
Finding the type of a value
SELECT TYPEOF ( expression/value );!
Rounding numbers
!
!
!
!
AGGREGATE DATA
Counting the number of rows
SELECT COUNT(*) FROM Table_Name;
SELECT COUNT(*) FROM Table_Name WHERE Column1 > 999 AND Column2 = 888;!
GROUP BY clause
SELECT Continent, COUNT(*)
FROM Table_Name
GROUP BY Continent
;!
!
!
!
!
!
!
!
!
The distinct keyword can be used with any of the aggregate functions. It's used to remove
duplicates before the aggregate function is called.
SELECT COUNT(DISTINCT HeadofState) AS Counter FROM Country ;!
TRANSACTIONS!
Transactional database operations can improve reliability and performance for larger or more
complex operations. The begin transaction statement tells the system that all the statements up
to the end transaction statement, are to be treated as one unit. And the end transaction
statement, ends that transaction and allows the system to commit those statements in that
transaction to storage.!
!
!
!
!
!
!
!
BEGIN TRANSACTION;!
!
ROLLBACK;!
!
BEGIN TRANSACTION;
END TRANSACTION!;!
!
!
TRIGGERS
Updating a table with a trigger
Trigger can be an excellent way to enforce business rules that require a table to be
updated whenever another table is updated.
!
!
!
Creating a view
We can use VIEW to store the result of a SELECT statement effectively as a table. Any
SELECT statement at all can be saved as a view.
CREATE VIEW trackView AS
SELECT id, album_id, title, track_number, duration / 60 AS m, duration % 60 AS s FROM
track_table ;
SELECT * FROM trackView treat VIEW as a normal table