Professional Documents
Culture Documents
Subqueries and Joins
Subqueries and Joins
Today, we are going to go through the steps of creating a database from scratch, as well as consider
some conceptual items. We will do most of this in PHPMyAdmin. For each step, make sure that you
write your query in PhpMyAdmin and then copy/paste it into this document BEFORE you execute it. You
must have the SQL queries for each step to receive full credit.
Phase 1
Step 1 – Using the following data table below, please write the SQL statement required to construct the
first table. Please name this table ‘PEOPLE’. Be sure to declare the correct ‘type’ variable for each
column of the table (int, char, etc.). Be sure to note that PersonID is in italics, so it is to be set as the
PRIMARY KEY for this table. Be sure to include this in your statement to create the table.
Step 2 – As you can see in the table above, ‘PersonID’ is the primary key for this table.
IMPLICIT:
INSERT INTO People
VALUES (4, 'Chuck Bass','Entrepreneur', 4), (5, 'Nate Archibald', 'Editor-in-chief', 1), (6, 'Samantha
Stephens', 'CEO', 'NULL');
Step 1 – Using the following data below, please write the SQL statement required to construct the
second table. Please name this table ‘UNIVERSITIES’. Be sure to declare the correct ‘type’ variable for
each column of the table. Also, be sure to declare UniversityID as a Primary Key.
Step 3 – Use an ALTER statement to modify the PEOPLE table to have ‘UniversityID’ become a foreign
key referencing ‘ID’ in the UNIVERSITIES table.
Step 1 – Nate Archibald has decided to leave his job at the newspaper and become mayor of New York
City. Write an update statement to change his occupation to Mayor, NYC.
UPDATE PEOPLE
SET Occupation = 'Mayor, NYC'
WHERE PersonID =5;
Phase 6 – SELECT
Step 1 – Write a SELECT statement to display the name and occupation of all individuals who go to
Columbia University.
Step 2 – Write a SELECT statement to display all of the data in the PEOPLE table, sorted in descending
order, by name.
SELECT * FROM PEOPLE
ORDER BY NAME DESC;
Step 3 – Write a SELECT statement to display the University Name, location, and tuition from the
UNIVERSITIES table sorted in ascending order by location.
Step 5 – Write a SELECT statement to display the university name and location of any university name
that ends with the word ‘University’. (Hint: Wild cards might be useful here)
**One and only one time that the entity can be associated with the related entity
Step 1 – Write a statement to display the number of distinct universities in the PEOPLE table.
Step 2 – Write a statement to display the average of all of the tuition rates in the UNIVERSITIES table.
SELECT AVG(Tuition)
FROM Universities;
Phase 9 – Subqueries
Step 1 – Write a subquery that displays the name and university from the PEOPLE table where that
person attending a university with an acceptance rate of less than 10%.
Step 3 – Write a subquery that displays the name, occupation, and university of any person who went to
a university that had a less than 10% acceptance rate, as well as a tuition below $50,000 per year.
Phase 10 – Joins
Step 1 – Write an INNER JOIN query that displays the name, occupation, university name, and tuition
cost for everyone that attended Columbia University.
Step 2 – Write a LEFT OUTER JOIN query that displays all universities and their alumni.
a) Are any records from the PEOPLE table not displayed? If so, which ones?
All 6 people are displayed.
Step 3 – Duplicate the query from Step 2, and simply change the LEFT OUTER JOIN to a RIGHT OUTER
JOIN. Describe what changed.
All of the names/alumni are not displayed, they are NULL values.