SQL Freecodecamp
https://www.youtube.com/watch?v=HXV3zeQKqGY
32 flashcards · Programming Languages · by charlie42
What's a surrogate key?
A primary key that doesn't map to any real world value
What's the opposite of surrogate key?
Natural key
What's a foreign key?
A key with values of a primary key of another table
What's a composite key?
A primary key consisting of multiple columns that uniquely identify a row. Often both are a foreign key.
What do we mean when we say SQL is a hybrid language?
Querying, schema definition, permission setting, and updating are four different sub-languages
List data types for numbers
INT DECIMAL
What are the parameters for DECIMAL data type
DECIMAL(M, N) M - total number of digits N - out of M, how many are after decimal point
What's the data type used for strings?
VARCHAR(M) M - number of characters
What are other common data types besides strings and numbers?
BLOB DATE TIMESTAMP
What BLOB stands for?
Binary Large OBject
What format is used for DATE data type?
YYYY-MM-DD
What format is used for TIMESTAMP data type?
YYYY-MM-DD HH:MM:SS
DQL: Display all information from a table
SELECT * FROM table;
When creating a table, other than PRIMARY KEY, how to specify that values in a column can't duplicate?
UNIQUE key word
DQL: sort values from highest to lowest
ORDER BY column DESC;
DQL: get only first n rows
LIMIT 2;
DQL: filter by value
WHERE column = 'value';
DQL: filter by several values
WHERE column = 'value' AND another_column = 'value';
Symbol for comment
--
Symbol for not equal to
<>
DQL: filter by a column containing one of several possible values (e.g. name is either Claire or Kate or Mike)
WHERE name IN ('Claire', 'Kate', 'Mike');
DQL: use an alias when printing a column
SELECT column AS col FROM table;
DQL: print all unique values from a column
SELECT DISTINCT name FROM table;
DQL: check how many non-null values there are in a column
SELECT COUNT(name) FROM table;
DQL: check mean of a column
SELECT AVG(age) FROM table;
DQL example: total sales done by each employee (same table)
SELECT SUM(sales), employee_id FROM table GROUP BY employee_id
DQL: filter using a wildcard
SELECT name FROM table WHERE name LIKE '%Jr%'
DQL: name two wildcard characters in SQL and what they're used for
% - any number of characters _ - exactly one character
DQL: print all names from table employee and all names from the table client in the same list
SELECT employee.name FROM employee UNION SELECT client.name FROM client;
DQL: print values from two tables with a common key
SELECT table1.name, table2.address FROM table1 JOIN table2 ON table1.id = table2.id
DQL: What kind of JOIN is the default?
Inner join
DQL: What's the syntax for filtering by a list of values that you got from another query
WHERE column IN ( SELECT name FROM table2 WHERE condition; )