SQL Basics Flashcards
(45 cards)
SELECT
Column you want to look at
use to specify the columns you want to retrieve data from
FROM
Table the data lives in
used to specify the table or tables you want to retrieve data from
WHERE
Certain conditions on the data
used to filter data based on specific conditions
GROUP BY
Column you want to aggregate by
used to group data based on one or more columns
ORDER BY
Column you want to order results by in ASC or DESC order
Used to sort data in ascending or descending order based on one or more columns. The ordering is automatically ASC and will order in sequence of your first statement.
SELECT DISTINCT
Returns unique values of specified columns. This can be useful to eliminate duplicate rows from your query results.
INNER JOIN
When you only want to see information from two tables that match.
HAVING
Certain condition on aggregation
LIMIT
Maximum order of rows you want your results to contain.
Syntax for INNER JOIN
SELECT Columns
FROM Table 1 INNER JOIN table 2
ON Table1 Col 1 = Table 2 col 2
Joins - USING instead of ON
USING has to be exact match, ON Specifys columns
Aliases and syntax
Aliases shorten names when you are refering to them in SQL
SELECT c.col1, c.col2, e.col1, e.col2
FROM Customer c INNER JOIN e
ON c.ID = e.ID
If you have to count the number of times a track appears in the table and group by track name. What would the statement be?
Table = Track
Track Name = Name
– Select the track name and count
SELECT t.Name, COUNT(*)
FROM Track t
– Filter on the USA
WHERE i.BillingCountry = ‘USA’
– Group by track name
GROUP BY t.name
What are the three types of outer joins?
- Left Outer Join
- Right Outer Join
- Full Outer Join
Left Outer Join
Returns matched rows and left table rows
Nulls will be on the right side of the table
Right Outer Join
Returns matched rows and right table rows
Nulls will be on the left side of the table
Full Outer Join
Returns all matched and unmatched rows from both tables
Nulls will be on both side of the table
Cartesian Product (Cross Product)
A cartesian product is when you have two sets of elements and you multiply them to generate all the possible pairs between the elements of the two sets.
SELF JOIN
Self joins allow you to join a table to itself.
How do you concatenate in a select statement?
SELECT e.firstname || ‘ ‘ || e.lastname AS “Employee”
output = new colume with full name
set operators
Take the output of two or more SELECT queries and combine them into one result.
Row oriented as they combine queries
set operators v joins
both combine data.
Join clauses combine tables and are column oriented.
Set operators combine queries and are row oriented.
4 types of set - operators
Union - The first is union and it returns all rows with no duplicates.
Union All - UNION ALL which returns all rows including duplicated
Intersect - returns rows outputted by both queries
Minus- returns distinct rows in the first query that are not in the second query.
Union
returns all distinct rows selected by either query, effectively combing the results and duplicates