SQLMastery Flashcards
What is a database?
database -
Collection of data stored in a format that can be easily accesssed
How do we manage our databases?
Using a software system -
Database Management System (DBMS)
How?
We connect to a DBMS, give it instructions like query / modify, DBMS executes instructions and sends back results
What two types of Database Management Systems (DBMS) do we have?
Relational and Non-Relational (noSQL)
How is data stored in relational databases?
In tables that are linked to eachother using relationships
Each table stores data about a specific object
SQL is language we use to work with these
What are the popular relational database management systems?
MySQL
SQL Server
Oracle
**all based on standard SQL specification
What is the most popular, open source database management system in the world?
MySQL
Used in this course!
Do noSQL (non relational database management systems) understand SQL?
NO!
NoSQL systems don’t understand SQL
They have their own query language!!
Where was SQL invented?
1970 at IBM
Structured English Query Language
What will we be learning in this course?
SQL (Query language) with MySQL (DB management)
What is covered in this course?
Essential Skills -
Retrieve, insert, update, delete data
(90% will apply to other DBMS)
Core skills -
summarize data, complex queries and subqueries, built in MySQL functions, views, stored procedues
(will apply to other DBMS)
Advanced skills -
triggers, events, transactions, concurrency
Top performer skills -
designing databases (systematic approach), indexing for high performance (speed queries), securing databases (user accounts, privledges)
**if you follow and complete excercise - can use SQL in any project and pass any interview questions
What is the first step to getting data from a database?
Select the database
query will be executed against database
Is SQL a case sensitive language?
No.
However, by convention:
SQL statments are uppercase,
lowercase for everything else
What is the SELECT clause?
Allows us to choose the columns
Select
* - all columns in a table
column_names - certain columns
How do we put a comment in SQL syntax?
double hyphen “ – “
Does order matter when using the SELECT statement (SELECT, FROM, WHERE, ORDER BY)?
Yes, it does:
SELECT – column or *
FROM – table name
WHERE –filter data
ORDER BY – column to order entries
**cannot change the order of these clauses or get syntax error
**FROM, WHERE, ORDER BY ARE OPTIONAL
Can we use arithmetic operators with SQL?
Yes.
How is math executed in SQL?
Using order of operations
If you want to change order, can use parentheses
How can we change the name of a result column?
AS keyword
can give alias
can use ’ ‘ around word if has parentheses
Ex. ‘discount factor’
What is the DISTINCT keyword?
Removes duplicates
What is a SQL excercise?
What is the WHERE clause?
Used to filter data
Conditional Operaters:
>
<
<=
=
!= or <>
What is an excercise using the WHERE statement?
What is the AND operator?
Used to filter data based on multiple conditions
WHERE (condition) AND (condition)
**BOTH Conditions are TRUE
What is the OR operator?
WHERE (condition) OR (condition)
satisfy one condition only!
How can we combine AND, OR and WHERE clauses?
Combining logical operators!
Query execution engine evaluates in order:
AND operator evaluated first!!
Can use ( ) to change order and make code cleaner!
What is the NOT operator?
Changes expression:
WHERE NOT (birth_date >=‘1990-01-01’ OR points > 1000)
Becomes (flips signs):
WHERE (birth_date <=‘1990-01-01’ AND points < 1000)
What is an excercise using logical operators?
Can use an arithmetic expression in logical operators
What is the IN operator?
like chaining OR statements
Compare an attribute to a list of values
What is an excercise using the IN operator?
What is the BETWEEN operator?
What is an excercise using the BETWEEN operator?
Can use BETWEEN operator with dates, not only numbers!
What is the LIKE operator?
Allows us to search for string patterns
% any number of characters
_ single character
How can we get all the customers whose last name ends in “y”?
LIKE %y
What is an excercise using LIKE?
What is another excercise using the LIKE keyword?
Get all the customers whose phone numbers end in 9
What is the REGEX keyword?
regular expression
more powerful than LIKE statement
^ represents beginning of a string
$ represents end of a string
[a-h] multiple characters
represents two search criteria
What are excercises using the REGEX syntax?
What is the NULL keyword?
allows us to search for null values
What is an excercise using the NULL keyword?
get all orders that have not been shipped
How can we sort data in our SQL queries?
What is an excercise using ORDER BY?
Write a query produces result:
item #2, sorted by total price of item descending order
What is the LIMIT clause?
Allows us to limit number of entries retrieved
LIMIT clause should always come at end!
ORDER matters!
SELECT
FROM
WHERE
ORDER BY
LIMIT
How can we offset using LIMIT clause?
What is an excercise using the LIMIT clause?
Get top 3 customers (highest points)!
What is the JOIN clause?
Let’s us join tables
JOIN (table) ON (condition)
How do we select specific columns across multiple tables using the JOIN clause?
qualify them with specific table
How can we simplify our code using alias?
What is an excercise using the JOIN clause?
join the order_items table with the products table
display order ID, product ID, quantity and price
How can we join across databases?
Real world db admin must do this!
prefix with the database name
What is a self JOIN?
Joining a table with itself!
HOW?
pre-fix each column with an alias
use a different alias for self join table
Ex. join employee table with itself to get the name of the manager
JOIN reports_to column with employee_id column in employees table
How do we join multiple tables?
In real world, you’ll join 10 tables!
Ex. Join
orders table
w/ customers table
w/ order_status table
What’s an excercise using multiple table JOINS?
Show payments with name of client, payment method
JOIN
payments table
w/ clients table
w/ payment_methods table
What are compound JOIN conditions?
When we have a composite primary key
cannot uniquely identify an order by one column, can use two combined to uniquely identify
Ex. Order_Id and product_ID are duplicated
What is another way to write this query using implicit JOIN syntax?
Better to use explicit JOIN syntax!
Two queries are equivalent (explicit and implicit JOIN syntax)
HOWEVER:
Mosh, doesn’t recommend using implicity JOIN syntax
WHY?
if forget WHERE clause, get a cross join
When do we use an outer JOIN?
OUTER keyword is optional
Ex. When we want to see all customers, not only customers who have an order (JOIN condition is based on Order table)
What two types of outer JOIN do we have?
As a best practice use LEFT joins
avoid RIGHT joins
WHY?
makes code cleaner, easier to read
WHAT?
OUTER keyword is optional
LEFT - all records from left table returned whether JOIN ON condition is true or not (null if not)
RIGHT - all records from right table
What’s an excercise using outer JOIN?
JOIN products table with order_items table
How can we solve this issue?
Replace inner JOIN with left JOIN
When do we use self outer JOIN?
When we are doing a self join
and want to see all entries (even those that do not match our criteria)
What is the USING clause?
replaces ON clause
ONLY WORKS when column name is exactly the same across different tables!
EX. Cannot use Orders.status = Order_Statuses.order_status_id
How can the USING clause replace a compound JOIN?
Excercise: use the USING clause to write a query that produces order_date, client_name, amount and payment_method.
What are NATURAL JOINS?
ALT. Syntax for JOINS
Replaces USING and ON clause
HOWEVER,
not recommended!
WHY?
Dangerous because letting DB engine guess the JOIN
Can produce unexpected RESULTS!
DISCOURAGED FROM USING!!
What is a CROSS JOIN?
Combine every record from first table with second table
Explicit Synax is more clear!!
Excercise: DO a CROSS JOIN between shippers and products
What is the UNION operator?
Can combine results from multiple queries
Across same table or different tables
CAUTION:
Queries must return same # of columns or you’ll get an error
Excercise: Write a Query to product this report (UNION).
How do we INSERT a row into a table?
What is another way to write this INSERT statement?
Don’t need to explicitly state DEFAULT or NULL
How can we INSERT multiple rows in a table?
EXCERCISE: INSERT three rows into the products table
How do we insert data into multiple tables?
How can we quickly copy data from one table into another?
How do we select a subset from a table to copy into another table?
What is a subquery?
a SELECT statement used in another query (ex. INSERT)
What’s a really cool excercise using INSERT?
EXCERCISE: Create a copy of the invoices with a date in a new table, in new table, display client name.
JOIN with clients table to display client_name in new table
How do we update a single row (record) in a table?
Undo:
reset payment_total = DEFAULT, payment_date = NULL
*DEFAULT value is set to 0, MySQL will set
*can insert NULL into columns that accept NULL values
Can we use an expression to update a value in a row?
Yes
How can we update multiple rows in a table?
What’s an excercise with updating multiple rows?
WRITE SQL statement:
Give any customer born befor 1990 an extra 50 points
How can we use subqueries in an UPDATE statement?
subquery = SELECT statement in another query
What is an excercise using a subquery?
Ex. set the status of a customer to gold if they have over 3000 points.
How can we delete data in SQL?
DELETE FROM table
WHERE condition
How do we recreate our databases?
execute .sql for creating databases
What is a function?
A piece of pre-built code we can re-use!
MySQL comes with a bunch of pre-built functions.
Ex. Aggregate Functions - take a series of values and aggregate to produce a single value
MAX ( )
MIN ( )
AVG ( )
SUM ( )
What are aggregate functions?
Pre-built code we can use!
Aggregate Functions -
take a series of values and aggregate to produce a single value
MAX ( )
MIN ( )
AVG ( )
SUM ( )
How can we find the MAX, MIN and AVG invoice total?
Use pre-built aggregate functions!
MAX ( )
MIN ( )
AVG ( )
Can we use MAX ( ) on non-numeric values?
Yes. Can use on dates and strings!
What is the COUNT function?
Returns count of items with non NULL values
Count(*) give all items count irrespective of NULL values
What is the SUM function?
give total
can use an expression
Ex. (invoice_total * 1.1) will multiple each row (value) by 1.1 then add them!
Can we use aggregate functions on filtered data?
Yes, using WHERE clause
How can we exclude duplicate data?
DISTINCT keyword
What is an excercise using aggregate functions?
Generate this table
What is the GROUP BY keyword using aggregate functions?
can see total sales per client
group by one or more columns
to group data
Ex. by client_id, total sales for each client
How can we get the total invoices per client for the first half of the year?
How can we see the total sales for each state and city?
What is an excercise using the GROUP BY clause?
When do we use the HAVING clause?
to filter data after we group our rows
What is the difference between the HAVING clause and the WHERE clause?
- the HAVING clause is used to filter data AFTER grouping
the WHERE clause is used to filter data BEFORE grouping
- HAVING clause the column must be included in SELECT clause
using a WHERE clause can select any column
How can we write a compound search condition in the HAVING clause?
can use one or more conditions
HAVING column must be included in SELECT clause
using a WHERE
How can we get the customers located in Virginia who have spent more than $100? (use HAVING clause)
As a rule of thumb:
whenever you have an aggregate function in a SELECT statement, grouping data, group by all columns in SELECT clause
This excercise very similar to REAL WORLD queries!!
What is the with ROLLUP operator?
powerful operator for summarizing data!!
Calculates summary for each group and entire result set
ONLY AVAILABLE IN MYSQL (NOT IN ORACLE, other DBMS!!)
Ex. sum for San Francisco, sum for all cities in CA, finally sum for all states (entire result set)
Write a query to produce this result (ROLLUP operator):
Total payments received for each payment method (cash, credit card, wire transfer, PayPal)
What is a subquery?
Can write subqueries in SELECT, FROM or WHERE clauses!
A statement within another SQL query
writing complex queries involves subqueries!!
Ex. get products more expensive than lettuce
How can we get all the products that are more expensive than lettuce?
subquery!
MySQL will evaluate subquery first, then return result to our main query!
How can we find the employees who earn more than average? (HINT: subquery)
How can we find the products that have never been ordered?
- Get unique list of products ordered from the order_items table (subquery)
- Get all products NOT IN subquery
**In this example, subquery is returning a list of objects!
(vs. single value)
How can we write a query to find the clients without invoices?
How can we re-write this subquery as a JOIN?
When should we use a JOIN vs. a subquery?
Performance vs. readability
Some queries get executed faster! (execution order)
query that is most readable!
Ex. Clients without invoices, first approach is more readable!
How can we find the customers who have ordered lettuce using both a subquery and a JOIN?
Second query is more readable
Why?
There is a natural relationship between customers, orders and order_items!
Filter clearly explains what customers!
How can we select all invoices larger than client 3’s max invoice? (HINT: ALL keyword)
subqueries!
ALL keyword
Subquery returns list of values
What is the ANY keyword?
= ANY is equivalent to IN
Ex. Select clients with at least two invoices
How can we write a query to return the employees whose salary is above average in their office?
for each employee, will execute subquery (returns value)
Correlated subquery!
What is a correlated subquery?
a subquery that references the outer query!
It’s executed multiple times! (for each row)
can be slow!
But very powerful!
Vs.
uncorrelated subquery - executed only once! value passed to outter query
How can we get the invoices that are larger than the client’s average invoice amount? (HINT: Correlated subquery)
What is the EXISTS operator?
Ex. select clients that have an invoice
For each client in client table, will check to see if there is a record that matches the subquery
Efficiency?
using the IN operator returns a list (could be millions long)
more efficient to use the EXISTS operator - doesn’t return a list, just a true/false, if true returns the query data
When is it more efficient to use the EXISTS operator vs. IN operator?
If the subquery after IN operator produces a large results set
more efficient to use EXISTS operator
WHY?
Using the EXISTS operator, subquery doesn’t return a result set to the outter query to the main query
How can we find the products that have never been ordered?
(HINT: use EXISTS operator)
For each product, checks if condition is true
if we have a product in order items table, subquery returns true, NOT operator becomes FALSE, item not in result set!
How can we use subqueries in the SELECT clause?
How can we get the total sales, average and difference for each customer?
SELECT clause subqueries
Virtual table!
Can store in memory and JOIN with other tables using client_id!
How do we use a subquery in the FROM clause?
Reserve SELECT clause in FROM clause for simple queries
Why?
Makes our main query complex
Views can replace this!
What is the ROUND function?
A built in function for working with numeric values
What is the TRUNCATE function?
What is the CEILING function?
Returns the smallest integer greater than or equal to a number
What is the FLOOR function?
returns largest integer less than or equal to number
What is the ABS function?
calculates absolute value of a number, always returning positive
What is the RAND ( ) function?
returns a random number between 0 - 1
What is the LENGTH ( ) function?
returns length of a string
What is the UPPER ( ) function?
also have LOWER ( ) convert to lowercase!