SQL Flashcards
What is SQL?
Structured Query Language is a special-purpose programming language designed for managing data held in a relational database system.
DDL
Data Definition Language: To create and modify the structure of the database
DML
Data Manipulation Language: To perform read, insert, update and delete operations on the data of the database
DCL
Data Control Language: To control the access of the data stored in the database
BigInt Storage in Bytes
8
Int Storage in Bytes
4
SmallInt Storage in Bytes
2
TinyInt Storage in Bytes
1
money Storage in Bytes
8
smallmoney Storage in Bytes
4
Char and Varchar
String data types of either fixed length or variable length
Nchar and Nvarchar
UNICODE string data types of either fixed length or variable length
UNIQUEIDENTIFIER
a 16 byte GUID / UUID
NULL
Means literally “Nothing”, the absence of any value
WHERE field IS NULL | WHERE field IS NOT NULL
SELECT
The select statement is at the heart of most SQL queries. It defines what result set should be returned by the query, and is almost always used in conjunction with the FROM clause, which defines what part(s9 of the database should be queried.
- the wildcard character
It’s used to select all available columns in a table
table.*
It’s used to select all values from a specific table
- wildcard character problems
Excess IO network load, memory use, and so on.
Potencial problem if you add more columns in the tables involved
When to use * in production environment
When using EXISTS, such as select A.col1, A.col2 from A where exists (select * from B where A.ID = b.A_ID). In COUNT(*) can be used.
Aliases
Column aliases are used mainly to shorten code and make column names more readable.
Aliases can be applied to Columns and table as well
WHERE
the condition can be any SQL expression, specified using comparison or logical operator like >, , >=, <=, LIKE, NOT, IN, BETWEEN
CASE
When results need to have some logic applied ‘on the fly’ one can user CASE statement to implement it.
SELECT case when col1 < 50 then ‘under’ else ‘over’ end threshold from tablename
Selecting without Locking the table
Sometimes when tables are used mostly (or only) for reads, indexing does not help anymore and every little bit counts, one might use selects without LOCK to improve performance.
MySQL
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT * FROM TableName;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
AVG
The AVG() aggregate function will return the avergae of values selected
MIN
The MIN() aggregate function will return the minimun of values selected.
MAX
The MAX aggregate function will return the maximun of values selected
COUNT
The COUNT() aggregate function will return the count of values selected
SUM
The SUM() aggregate function will retrun the SUM of the values selected for all rows
How filter results after grouping?
If you need to filter the results after the grouping has been done, you need to use the HAVING clause.
Embed a CASE statement in the ORDER BY clause
ORDER BY CASE WHEN LName=’Jones’ then 0 ELSE 1 END ASC
DISTINCT
Select unique values only
GROUP BY
Results of a SELECT query can be grouped by one or more columns suing the GROUP BY statement.
All results with the same value in the grouped columns are aggregated together.
This generates a table of partial results, instead of one result.
GROUP BY can be used in conjunction with aggregation functions using having statement to define how non-grouped columns are aggregated.
GROUP BY results using a HAVING
A HAVING clause filters the results of a GROUP BY expression.
ORDER BY
You can use a colum’s number to indicate wich column to base the sort on, instead of describing the column by its name
ORDER BY Customized sorting order
ORDER BY CASE Department WHEN 'HR' THEN 1 WHEN 'Accountant THEN 2 ELSE 3 END;
CASE
The case expression is used to implement if-then logic.
CASE Shorthand
Evaluate and expression against a series of values. CASE Price WHEN 5 THEN 'Cheap' WHEN 15 THEN 'Affordable' ELSE 'Expensive' END AS PriceRating
LIKE
The % wildcard appended to the beggining or end (or both) of a string will allow 0 or more of any character before the beggining of after the end of the pattern to match
All records where FName 3rd character is ‘n’ from Employees
select * from Employees WHERE FName LIKE ‘__n%’
Single character match
The _ (underscore) character can be used as a wildcard for any single character in a pattern match