WHERE
1 | SELECT * FROM Table WHERE ColumnName <> 'Some value'. |
This Query will not return Null values.
To return Null values we need to check if Column is NULL
1 | SELECT * FROM Table WHERE ColumnName IS NULL |
LIKE
If a column has type varchar(50)
, and in one row its value doesn’t have length 50. Spaces will be added to the end.
So WHERE ColumnName LIKE '%SomeValue'
will return nothing. You can write the query like this. WHERE ColumnName LIKE '%SomeValue%'
Functions
- LEFT(): return the left most char from string
- RIGHT(): return the right most char from string
- LTRIM(): remove the spaces on the left of string
- RTRIM(): remove the spaces on the right of string
GROUP BY and HAVING
GROUP BY will group values into different groups. HAVING can filter out some values after GROUP BY.
JOIN
INNER JOIN
The default JOIN, rows will be returned if they appear in both tables
CROSS JOIN
Will return the combination of rows from Table A and Table B. If Table A has 4 rows and Table B has 10 row, it will return 40 rows.
OUTER JOIN
- LEFT OUTER JOIN (LEFT JOIN)
The OUTER keyword is optional. LEFT OUTER JOIN is the same as LEFT JOIN. Values will be returned if it appear in the LEFT table. It doesn’t need to be in the RIGHT table.
- RIGHT OUTER JOIN (RIGHT JOIN)
RIGHT OUTER JOIN is the same as RIGHT JOIN. Values will be returned if it appear in the RIGHT table. It doesn’t need to be in the LEFT table.
- FULL OUTER JOIN (FULL JOIN)
Values will be returned if they appear in either LEFT table or RIGHT table.
UNION
UNION can be placed between SELECT queries.
Rows will be appended for each SELECT.
Each SELECT must have same number of Columns and DataType needs to match.
UNION ALL
If SELECT queries return same rows, they will all be returned.
INSERT
INSERT SELECT
The values returned from SELECT will be inserted immediately. Works will multiple rows.
1 | INSERT INTO TableA(ColumnA, ColumnB) |
UPDATE and DELETE
If we want to delete some value in a row, we could SET it to NULL
1 | UPDATE Table SET ColumnName = NULL WHERE RowId = 1 |
CREATE TABLE
1 | CREATE TABLE TableName |
CREATE VIEW
1 | CREATE VIEW ViewName AS |
VIEW is a temp Table, it can save complex SELECT queries and can be reused later.
TRANSACTION
If there is error in the middle of a TRANSACTION, it will not COMMIT
1 | BEGIN TRANSACTION |
SAVEPOINT and ROLLBACK
If error happens in the middle of a TRANSACTION and we don’t want to ROLLBACK to the start. We can create SAVEPOINT and let it ROLLBACK to that point.
1 | BEGIN TRANSACTION |
In the above code, if the second INSERT failed, @@ERROR will return a non-zero value, it will ROLLBACK to SAVEPOINT PointOne.
Constraint
- Primary key
1 | CREATE TABLE Table |
- Foreign key
1 | CREATE TABLE Table |
Foreign Key values must come from Primary key in the other table.
Primary key records cannot be deleted unless all Foreign key records were deleted first.
Some DBMS support CASCADE DELETE, which will delete the Primary key record and related Foreign key record in other tables.
- UNIQUE
One table could have multiple UNIQUE Constraint.
- CHECK
Further restrict values in this Column
1 | CREATE TABLE Table |
INDEX
If you create an index on a Column, DB will sort this Column and store it. Next time you SELECT by this Column, DB will search faster (binary search) because it is sorted.
But add index to a Column will decrease the efficiency of doing UPDATE/INSERT/DELETE on those Columns. Because DB needs to update INDEX on those Columns.
1 | CREATE INDEX Table_Column_Index |
TRIGGER
TRIGGER will be execute when certain changes happen to a table
1 | CREATE TRIGGER Table_trigger |