SQL Basics
SQL Basics
● UNIQUE: Applying this constraint to a column ensures that no two entries in that
column are identical.
● NOT NULL: This constraint ensures that a column doesn’t have any NULL entries.
● PRIMARY KEY: A combination of UNIQUE and NOT NULL, the PRIMARY KEY
constraint ensures that no entry in the column is NULL and that every entry is
distinct.
● FOREIGN KEY: A FOREIGN KEY is a column in one table that refers to the PRIMARY
KEY of another table. This constraint is used to link two tables together. Entries to
the FOREIGN KEY column must already exist in the parent PRIMARY KEY column
for the write process to succeed.
● CHECK: This constraint limits the range of values that can be entered into a
column. For example, if your application is intended only for residents of Alaska,
you could add a CHECK constraint on a ZIP code column to only allow entries
between 99501 and 99950.
SQL FUNCTIONALITY:
ORDER BY questions
● Write a query to return the 10 earliest orders in the orders table. Include the id,
occurred_at, and total_amt_usd.
SELECT id, occurred_at, total_amt_usd
FROM orders
ORDER BY occurred_at
LIMIT 10;
● Write a query to return the top 5 orders in terms of largest total_amt_usd. Include the
id, account_id, and total_amt_usd.
SELECT id, account_id, total_amt_usd
FROM orders
ORDER BY total_amt_usd DESC
LIMIT 5;
● Write a query that displays the order ID, account ID, and total dollar amount for all the
orders, sorted first by the account ID (in ascending order), and then by the total dollar
amount (in descending order).
SELECT id, account_id, total_amt_usd
FROM orders
ORDER BY account_id, total_amt_usd DESC;
● Now write a query that again displays order ID, account ID, and total dollar amount for
each order, but this time sorted first by total dollar amount (in descending order), and
then by account ID (in ascending order).
SELECT id, account_id, total_amt_usd
FROM orders
ORDER BY total_amt_usd DESC, account_id;
● Compare the results of these two queries above. How are the results different when you
switch the column you sort on first?
In query #1, all of the orders for each account ID are grouped together, and then within
each of those groupings, the orders appear from the greatest order amount to the least.
In query #2, since you sorted by the total dollar amount first, the orders appear from
greatest to least regardless of which account ID they were from. Then they are sorted by
account ID next. (The secondary sorting by account ID is difficult to see here, since only
if there were two orders with equal total dollar amounts would there need to be any
sorting by account ID.)
WHERE Questions
● Pulls the first 5 rows and all columns from the orders table that have a dollar amount of
gloss_amt_usd greater than or equal to 1000.
SELECT *
FROM orders
WHERE gloss_amt_usd >= 1000
LIMIT 5;
● Pulls the first 10 rows and all columns from the orders table that have a
total_amt_usd less than 500.
SELECT *
FROM orders
WHERE total_amt_usd < 500
LIMIT 10;
● Write a query that finds the percentage of revenue that comes from poster paper
for each order. You will need to use only the columns that end with _usd. (Try to
do this without using the total column.) Display the id and account_id fields
also.
● All companies whose names contain the string 'one' somewhere in the name.
SELECT name
FROM accounts
WHERE name LIKE '%one%';
● Use the web_events table to find all information regarding individuals who were
contacted via the channel of organic or adwords.
SELECT *
FROM web_events
WHERE channel IN ('organic', 'adwords');
● Use the web_events table to find all information regarding individuals who were
contacted via any method except using organic or adwords methods.
SELECT *
FROM web_events
WHERE channel IN ('organic','adwords');
● All companies whose names do not contain the string 'one' somewhere in the name.
SELECT name
FROM accounts
WHERE name NOT LIKE '%one%';
SELECT name
FROM accounts
WHERE name NOT LIKE '%s';
Find all the company names that start with a 'C' or 'W', and the primary contact
contains 'ana' or 'Ana', but it doesn't contain 'eana'.
SELECT *
FROM account
WHERE (name LIKE 'C%' OR name LIKE 'W%')AND ((primary_poc LIKE '%ana%' OR
primary_poc LIKE '%Ana%') AND primary_poc NOT LIKE '%eana%');
Recap
Commands
You have already learned a lot about writing code in SQL! Let's take a moment to recap
all that we have covered before moving on:
Statement How to Use It Other Details
SELECT SELECT Col1, Col2, ... Provide the columns you want
FROM FROM Table Provide the table where the columns exist
LIKE WHERE Col LIKE '%me%' Only pulls rows where column has 'me'
within the text
IN WHERE Col IN ('Y', 'N') A filter for only rows with column of 'Y' or
'N'
NOT WHERE Col NOT IN ('Y', NOT is frequently used with LIKE and IN
'N')
AND WHERE Col1 > 5 AND Col2 Filter rows where two or more conditions
<3 must be true
OR WHERE Col1 > 5 OR Col2 Filter rows where at least one condition
<3 must be true
BETWEE WHERE Col BETWEEN 3 Often easier syntax than using an AND
N AND 5
Other Tips
Though SQL is not case sensitive (it doesn't care if you write your statements as all
uppercase or lowercase), we discussed some best practices. The order of the key
words does matter! Using what you know so far, you will want to write your statements
as:
SELECT col1, col2
FROM table1
WHERE col3 > 5 AND col4 LIKE '%os%'
ORDER BY col5
LIMIT 10;
Notice, you can retrieve different columns than those being used in the ORDER BY and
WHERE statements. Assuming all of these column names existed in this way (col1,
col2, col3, col4, col5) within a table called table1, this query would run just fine.
JOIN
If we wanted to only pull individual elements from either the orders or accounts table,
we can do this by using the exact same information in the FROM and ON statements.
However, in your SELECT statement, you will need to know how to specify tables and
columns in the SELECT statement:
● The table name is always before the period.
● The column you want from that table is always after the period.
For example, if we want to pull only the account name and the dates in which that
account placed an order, but none of the other columns, we can do this with the
following query:
SELECT accounts.name, orders.occurred_at
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;
This query only pulls two columns, not all the information in these two tables.
Alternatively, the below query pulls all the columns from both the accounts and orders
table.
SELECT *
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;
And the first query you ran pull all the information from only the orders table:
SELECT orders.*
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;
Joining tables allows you access to each of the tables in the SELECT statement
through the table name, and the columns will always follow a . after the table name
Alternatively, we can create a SELECT statement that could pull specific columns from
any of the three tables. Again, our JOIN holds a table, and ON is a link for our PK to
equal the FK.
To pull specific columns, the SELECT statement will need to specify the table that you
are wishing to pull the column from, as well as the column name. We could pull only
three columns in the above by changing the select statement to the below, but
maintaining the rest of the JOIN information:
SELECT web_events.channel, accounts.name, orders.total
● Provide a table for all the for all web_events associated with account name of
Walmart. There should be three columns. Be sure to include the primary_poc,
time of the event, and the channel for each event. Additionally, you might
choose to add a fourth column to assure only Walmart events were chosen.
SELECT a.primary_poc, w.occurred_at, w.channel, a.name
FROM web_events w
JOIN accounts a
ON w.account_id = a.id
WHERE a.name = 'Walmart';
● Provide a table that provides the region for each sales_rep along with their
associated accounts. Your final table should include three columns: the region
name, the sales rep name, and the account name. Sort the accounts
alphabetically (A-Z) according to account name.
SELECT r.name region, s.name rep, a.name account
FROM sales_reps s
JOIN region r
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
ORDER BY a.name;
OIN Check In
INNER JOINs
Notice every JOIN we have done up to this point has been an INNER JOIN. That is, we
have always pulled rows only if they exist as a match across two tables.
Our new JOINs allow us to pull rows that might only exist in one of the two tables. This
will introduce a new data type called NULL. This data type will be discussed in detail in
the next lesson.
Quick Note
You might see the SQL syntax of
LEFT OUTER JOIN
OR
RIGHT OUTER JOIN
These are the exact same commands as the LEFT JOIN and RIGHT JOIN we learned
about in the previous video.
OUTER JOINS
The last type of join is a full outer join. This will return the inner join result set, as well as
any unmatched rows from either of the two tables being joined.
Again this returns rows that do not match one another from the two tables. The use
cases for a full outer join are very rare.
You can see examples of outer joins at the link here and a description of the rare use
cases here. We will not spend time on these given the few instances you might need to
use them.
Similar to the above, you might see the language FULL OUTER JOIN, which is the
same as OUTER JOIN.