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

Reference Guide

Week 1:
Our sample table columns for this course:
StudentID
FirstName
LastName
Major
Minor
Class

SELECT *
The following SQL statement selects all of the columns and all of the rows from the "Students"
table by using the * character:
SELECT *
FROM Students;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
2 Robert Jones Law Medicine 2010
3 Susan Dawson Business Computer
Science
2014
4 Michael Martin Fine Arts Law 2012
5 Carolyn Fisher Medicine Fine Arts 2014
6 Douglas Souza Medicine Business 2016
7 Ellen Harrison Law Computer
Science
2014
8 Brayden Downey Business Fine Arts 2011
9 Colby Damon Computer
Science
Medicine 2014
10 Candace Kennedy Fine Arts Law 2013
11 James Matthews Medicine Law 2014
12 Raquel Walter Fire Science Business 2015
13 Carlos Santos Computer
Science
Medicine 2014


SELECT column names
The following SQL statement selects all of the columns and all of the rows from the "Students"
table by using the column names:
SELECT StudentID, FirstName, LastName, Major, Minor, Class
FROM Students;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
2 Robert Jones Law Medicine 2010
3 Susan Dawson Business Computer
Science
2014
4 Michael Martin Fine Arts Law 2012
5 Carolyn Fisher Medicine Fine Arts 2014
6 Douglas Souza Medicine Business 2016
7 Ellen Harrison Law Computer
Science
2014
8 Brayden Downey Business Fine Arts 2011
9 Colby Damon Computer
Science
Medicine 2014
10 Candace Kennedy Fine Arts Law 2013
11 James Matthews Medicine Law 2014
12 Raquel Walter Fire Science Business 2015
13 Carlos Santos Computer
Science
Medicine 2014

SELECT DISTINCT
The following SQL statement selects only the distinct values from the "Major" columns from the
"Students" table:
SELECT DISTINCT Major
FROM Students;
RESULT SET (rows will not necessarily be in this order):
Major
Medicine
Law
Business
Fine Arts
Fire Science
Computer Science


Week 2:
Our sample table columns for this course:
StudentID
FirstName
LastName
Major
Minor
Class

WHERE = Clause
The following SQL statement selects all the Students from the Major "Fine Arts", in the
"Students" table:
SELECT *
FROM Students
WHERE Major = 'Fine Arts';
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
5 Carolyn Fisher Medicine Fine Arts 2014
8 Brayden Downey Business Fine Arts 2011


WHERE <> Clause
The following SQL statement selects all the Students who are NOT in the graduating class of
2014, in the "Students" table:
SELECT *
FROM Students
WHERE Class <> 2014;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
2 Robert Jones Law Medicine 2010
4 Michael Martin Fine Arts Law 2012
6 Douglas Souza Medicine Business 2016
8 Brayden Downey Business Fine Arts 2011
10 Candace Kennedy Fine Arts Law 2013
12 Raquel Walter Fire Science Business 2015


WHERE > Clause
The following SQL statement selects all the Students who are in a graduating class after 2014, in
the "Students" table:
SELECT *
FROM Students
WHERE Class > 2014;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
6 Douglas Souza Medicine Business 2016
12 Raquel Walter Fire Science Business 2015


WHERE < Clause
The following SQL statement selects all the Students who were in a graduating class before
2014, in the "Students" table:
SELECT *
FROM Students
WHERE Class < 2014;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
2 Robert Jones Law Medicine 2010
4 Michael Martin Fine Arts Law 2012
8 Brayden Downey Business Fine Arts 2011
10 Candace Kennedy Fine Arts Law 2013


WHERE >= Clause
The following SQL statement selects all the Students who are in a graduating class in or after
2014, in the "Students" table:
SELECT *
FROM Students
WHERE Class >= 2014;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
3 Susan Dawson Business Computer
Science
2014
5 Carolyn Fisher Medicine Fine Arts 2014
6 Douglas Souza Medicine Business 2016
7 Ellen Harrison Law Computer
Science
2014
9 Colby Damon Computer
Science
Medicine 2014
11 James Matthews Medicine Law 2014
12 Raquel Walter Fire Science Business 2015
13 Carlos Santos Computer
Science
Medicine 2014


WHERE <= Clause
The following SQL statement selects all the Students who were in a graduating class in or before
2014, in the "Students" table:
SELECT *
FROM Students
WHERE Class <= 2014;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
2 Robert Jones Law Medicine 2010
3 Susan Dawson Business Computer
Science
2014
4 Michael Martin Fine Arts Law 2012
5 Carolyn Fisher Medicine Fine Arts 2014
7 Ellen Harrison Law Computer
Science
2014
8 Brayden Downey Business Fine Arts 2011
9 Colby Damon Computer
Science
Medicine 2014
10 Candace Kennedy Fine Arts Law 2013
11 James Matthews Medicine Law 2014
13 Carlos Santos Computer
Science
Medicine 2014


WHERE BETWEEN Clause
The following SQL statement selects all the Students who were in a graduating class between
2011 and 2014, in the "Students" table
SELECT *
FROM Students
WHERE Class BETWEEN 2011 and 2014;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
3 Susan Dawson Business Computer
Science
2014
4 Michael Martin Fine Arts Law 2012
5 Carolyn Fisher Medicine Fine Arts 2014
7 Ellen Harrison Law Computer
Science
2014
8 Brayden Downey Business Fine Arts 2011
9 Colby Damon Computer
Science
Medicine 2014
10 Candace Kennedy Fine Arts Law 2013
11 James Matthews Medicine Law 2014
13 Carlos Santos Computer
Science
Medicine 2014


WHERE IN Clause
The following SQL statement selects all the Students who were in one of the graduating classes
of 2010, 2012, or 2016, in the "Students" table
SELECT *
FROM Students
WHERE Class IN (2010, 2012, 2016);
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
2 Robert Jones Law Medicine 2010
4 Michael Martin Fine Arts Law 2012
6 Douglas Souza Medicine Business 2016


WHERE LIKE Clause
The following SQL statement selects all the Students who are in a major that contains the text
SCIENCE, in the "Students" table
SELECT *
FROM Students
WHERE Major LIKE %SCIENCE%;
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
3 Susan Dawson Business Computer
Science
2014
7 Ellen Harrison Law Computer
Science
2014


AND Operator
We can add the AND operator to our SQL.
The following SQL statement selects all Students from the Major "Medicine" AND the Minor
"Law", in the "Students" table:
SELECT *
FROM Students
WHERE Major='Medicine'
AND Minor='Law';
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
11 James Matthews Medicine Law 2014


OR Operator
We can add the OR operator to our SQL.
The following SQL statement selects all Students from the Minor "Law" OR "Business", in the
"Students" table:
SELECT *
FROM Students
WHERE Minor='Law'
OR Minor='Business';
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
6 Douglas Souza Medicine Business 2016
10 Candace Kennedy Fine Arts Law 2013
11 James Matthews Medicine Law 2014
12 Raquel Walter Fire Science Business 2015


AND & OR Operators
We can also combine AND and OR.
The following SQL statement selects all Students from the Major "Medicine" AND the Minor
must be equal to "Law" OR "Business", in the "Students" table:
SELECT *
FROM Students
WHERE Major='Medicine'
AND (Minor='Law' OR Minor='Business');
RESULT SET (rows will not necessarily be in this order):
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
6 Douglas Souza Medicine Business 2016
11 James Matthews Medicine Law 2014


Week 3:
Our sample table columns for this course:
StudentID
FirstName
LastName
Major
Minor
Class

ORDER BY
We can order our results in ascending order by a particular column or columns.
The following SQL statement selects all Medicine Students from the "Students" table, sorted by
the "StudentID" column:
SELECT *
FROM Students
WHERE Major='Medicine'
ORDER BY StudentID;
RESULT SET:
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
5 Carolyn Fisher Medicine Fine Arts 2014
6 Douglas Souza Medicine Business 2016
11 James Matthews Medicine Law 2014


ORDER BY DESC
We can also order our results in descending order instead of ascending order.
The following SQL statement selects all Medicine Students from the "Students" table, sorted
DESCENDING by the "StudentID" column:
SELECT *
FROM Students
WHERE Major='Medicine'
ORDER BY StudentID DESC;
RESULT SET:
StudentID FirstName LastName Major Minor Class
11 James Matthews Medicine Law 2014
6 Douglas Souza Medicine Business 2016
5 Carolyn Fisher Medicine Fine Arts 2014
1 Jane Smith Medicine Law 2011



ORDER BY Multiple Columns
We can also order our results by multiple columns.
The following SQL statement selects all Medicine Students from the "Students" table, sorted by
the "Major" and then the "Class" and then the LastName columns:
SELECT *
FROM Students
WHERE Major='Medicine'
ORDER BY Major, Class, LastName;

RESULT SET:
StudentID FirstName LastName Major Minor Class
1 Jane Smith Medicine Law 2011
5 Carolyn Fisher Medicine Fine Arts 2014
11 James Matthews Medicine Law 2014
6 Douglas Souza Medicine Business 2016

You might also like