Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

IST 210 – In-Class FUNTIVITIES 2: The RE-FUNENING

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 – Open PHPMyAdmin.


Step 2 – DELETE/DROP your PEOPLE and UNIVERSITIES tables from your database if they exist. Do not
skip this step.
Step 3 – Applaud your success.

drop table people;


drop table universities;

Phase 2 – CREATE TABLE

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.

PersonID Name Occupation UniversityID


1 Serena van der Woodsen Philanthropist 1
2 Blair Waldorf Fashion Designer 5
3 Dan Humphrey Author 2
4 Chuck Bass Entrepreneur 4
5 Nate Archibald Editor-in-chief 1
6 Samantha Stephens CEO null

Step 2 – As you can see in the table above, ‘PersonID’ is the primary key for this table.

CREATE TABLE People(


PersonID INT(3)PRIMARY KEY NULL,
Name VARCHAR(50) NOT NULL,
Occupation VARCHAR(50) NOT NULL,
UniversityID VARCHAR(10) NOT NULL
)

Phase 3 – INSERT Data


Step 1 – Create 5 INSERT statements to move all of this data into the table you have just created. For
additional practice, create 3 of these INSERT statements with explicit declarations of columns the data is
to go into, and for 2 of these INSERT statements, insert the data without explicit declarations.
EXPLICIT:

INSERT INTO PEOPLE


(PersonID, Name, Occupation, UniversityID)
Values (1, 'Serena van der Woodsen', 'Philanthropist', 1),
(2, 'Blair Waldorf', 'Fashion Designer', 5),
(3, 'Dan Humphrey', 'Author', 2);

IMPLICIT:
INSERT INTO People
VALUES (4, 'Chuck Bass','Entrepreneur', 4), (5, 'Nate Archibald', 'Editor-in-chief', 1), (6, 'Samantha
Stephens', 'CEO', 'NULL');

Phase 4 – CREATE TABLE

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.

CREATE TABLE Universities(


ID INT(3)PRIMARY KEY NULL,
UniversityName VARCHAR(75) NOT NULL,
Location VARCHAR(50) NOT NULL,
President VARCHAR(10) NOT NULL,
Tuition VARCHAR(15) NOT NULL,
AcceptanceRate INT (3) NOT NULL
)

ID UniversityName Location President Tuition AcceptanceRate


1 Columbia University New York, NY Lee C. Bollinger 51,008 7
2 New York University New York, NY Andrew D. Hamilton 46,170 35
3 Slippery Rock University Slippery Rock, PA Cheryl Norton 12,719 66
4 University of Phoenix Null John Sperling 5,000 100
5 Yale University New Haven, CT Peter Salovey 47,600 6

Step 2 – Insert the data above into the UNIVERSITIES table.


INSERT INTO Universities
(ID, UniversityName, Location, President, Tuition, AcceptanceRate)
Values (1, 'Columbia University', 'New York, NY', 'Lee C. Bollinger', '51,008', 7),
(2, 'New York University', 'New York, NY', 'Andrew D. Hamilton', '46,170', 35),
(3, 'Slippery Rock University', 'Slippery Rock, PA', 'Cheryl Norton', '12,719', 66),
(4, 'University of Phoenix', 'null', 'John Sperling', '5,000', 100),
(5, 'Yale University', 'New Haven, CT', 'Peter Salovey', '47,600', 6);

Step 3 – Use an ALTER statement to modify the PEOPLE table to have ‘UniversityID’ become a foreign
key referencing ‘ID’ in the UNIVERSITIES table.

ALTER TABLE PEOPLE ADD FOREIGN KEY (UniversityID) REFERENCES Universities(ID);

Phase 5 – UPDATE DATA

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.

SELECT Name, Occupation


FROM People
WHERE 'UniversityName' = '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.

SELECT UniversityName, Location, Tuition


FROM Universities
ORDER BY 'Location' ASC;
Step 4 – Write a SELECT statement to display the university name, location, and acceptance rate of any
university with an acceptance rate greater than 10%.

SELECT ID, UniversityName, Location, AcceptanceRate


FROM Universities
WHERE AcceptanceRate >= 10;

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)

SELECT * FROM Universities


WHERE UniversityName LIKE '%university';

Phase 7 – Some Conceptual Stuff

Step 1 – What does SQL stand for? Structured Query Language


Step 2 – What does this notation mean?

**One and only one time that the entity can be associated with the related entity

Phase 8 – Built-in Functions

Step 1 – Write a statement to display the number of distinct universities in the PEOPLE table.

SELECT DISTINCT 'UniversityName' FROM People;

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%.

SELECT 'Name', 'UniversityName'


FROM PEOPLE
WHERE 'AcceptanceRate' in
(SELECT 'AcceptanceRate'
FROM People
WHERE 'AcceptanceRate' < 10);
Step 2 – Write a subquery that displays the name and university from the PEOPLE table where that
person attends a university in New York, NY.

SELECT 'Name', 'UniversityName'


FROM PEOPLE
WHERE 'Location' in
(SELECT 'Location'
FROM People
WHERE 'Location' = 'New York, NY');

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.

SELECT 'Name', 'Occupation', 'UniversityName'


FROM PEOPLE
WHERE ('AcceptanceRate', 'Tuition') IN
(SELECT 'AcceptanceRate', 'Tuition'
FROM People
WHERE 'AcceptanceRate' < 10
AND 'Tuition' < '50,000');

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.

SELECT People.Name, People.Occupation, Universities.UniversityName, Universities.Tuition


FROM People
INNER JOIN Universities ON People.UniversityID = Universities.ID WHERE People.UniversityID = 1;

Step 2 – Write a LEFT OUTER JOIN query that displays all universities and their alumni.

SELECT People.Name, Universities.UniversityName


From People
LEFT JOIN Universities ON People.Name = Universities.UniversityName;

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.

SELECT People.Name, Universities.UniversityName


From People
RIGHT JOIN Universities ON People.Name = Universities.UniversityName;

You might also like