SQL
Keywords
– A word reserved for use by a programming language or
scripting language.
- Has special meaning to the programming language/script
- Can not be used in the program except within the proper
syntax as intended by the language/script
INSC 20263 (BIS)
October 4, 2020 1
Terry Gray
SQL
Data Type
- A keyword used to specify a container (variable) to hold a
specific type of data
- Allows the language/script to understand how to interpret the
data being described
- Also used to specify the size of the data container
INSC 20263 (BIS)
October 4, 2020 2
Terry Gray
SQL
Why Data Types
Data Types are needed so the language/script can understand how
to treat the data
Numeric Values Added: Text Strings Added:
Integer myNum1 = 10; String myNum1 = “10”;
Integer myNum2 = 20; String myNum2 = “20”;
Integer myNum3 = myNum1 + myNum2; String myNum3 = myNum1 + myNum2;
String myNum4 = “Terry” + “Gray”
Outcome is myNum3 = 30
Outcome is myNum3 = “1020”
Outcome is myNum4 = “TerryGray”
INSC 20263 (BIS)
October 4, 2020 3
Terry Gray
SQL
Data Types
integer, int, numeric, smallint
- specifies the data stored in the column is a number
- smallint is a smaller number requiring less memory (2 bytes vs. 4)
string, char
- Character string, i.e., text field
- string data type is a variable length string
- char(n) where n is a number specifies a fixed length set of characters, e.g.,
char(50) is a column that can contain up to 50 characters and no more
date
- Stores calendar dates dd-Mon-yy, e.g., 18-Oct-22
INSC 20263 (BIS)
October 4, 2020 4
Terry Gray
SQL
Commands
CREATE TABLE
INSERT
INSC 20263 (BIS)
October 4, 2020 5
Terry Gray
SQL
Commands
SELECT – Used to query data from the database
WHERE - Used to filter a query result
INSC 20263 (BIS)
October 4, 2020 6
Terry Gray
SQL
Commands
UPDATE – Used to update existing data in a table
SUM
INSC 20263 (BIS)
October 4, 2020 7
Terry Gray
SQL
Commands
JOIN / ON – Used to create a temporary table by joining tables according to their relational links (foreign key
-> primary key)
SELECT Paper.PaperTitle, Researcher.Resname FROM Paper JOIN
Researcher ON Paper.ResearcherID = Researcher.ResearcherID
INSC 20263 (BIS)
October 4, 2020 8
Terry Gray
SQL
JOIN / ON – Used to create a temporary table by joining tables according to their relational links (foreign key
-> primary key)
INSC 20263 (BIS)
October 4, 2020 9
Terry Gray
SQL
Keyword Summary
SELECT - returns data FROM the specified table. Can only have one select statement per SQL
command
SELECT column1, column2, … FROM table_name
SELECT student, exam1, exam2 FROM grade_book
WHERE – filters data being returned to limit the output based on a condition ( >, <, =, >=, <= ),
applied after a select statement
SELECT column1, column2, … FROM table_name WHERE columnX >= value
SELECT student, exam1, exam2 FROM grade_book WHERE exam1 > 90
SELECT student, exam1, exam2 FROM grade_book WHERE exam3 = 90
SELECT student, exam1, exam2 FROM grade_book WHERE student = ‘Apple’
*Note, text strings must always be referenced in single quotes and be treated as case sensitive.
INSC 20263 (BIS)
October 4, 2020 10
Terry Gray
SQL
Keyword Summary
AND / OR – used to combine logical operations when filtering with WHERE for multiple values
Filter on same column for two required conditions
SELECT column1, column2, … FROM table_name
WHERE columnX >= value1 AND columnX < value2
Filter on different columns for two required conditions
SELECT column1, column2, … FROM table_name
WHERE columnX < value1 AND columnY < value2
Filter on different columns for one condition OR another
SELECT column1, column2, … FROM table_name
WHERE columnX < value1 OR columnY < value2
Filter on multiple columns for multiple conditions
SELECT column1, column2, … FROM table_name
WHERE columnX < value1 OR (columnY < value2 AND columnZ = ‘America’)
INSC 20263 (BIS)
October 4, 2020 11
Terry Gray
SQL
Keyword Summary
AVG / SUM / COUNT – averages, sums or counts the values in the column specified, AVG /
SUM / COUNT always comes after SELECT and before FROM
Average exam grades for exam 1
SELECT AVG(exam1)FROM table_name
Sum the total rainfall for days where temperature was above 90 degress
SELECT SUM(rainfall)FROM table_name WHERE temperature > 90
Count number of days temperature was below freezing
SELECT COUNT(temperature)FROM table_name WHERE temperature < 32
INSC 20263 (BIS)
October 4, 2020 12
Terry Gray
SQL
Keyword Summary
ORDER BY – sorts the data returned by the SELECT function
Ascending order use ASC
Descending order use DESC
DISTINCT - removes duplicate data sets from the data returned by the SELECT function
SELECT DISTINCT * FROM table_name
GROUP BY – allows operations to be grouped together on specific data
SELECT COLUMN_NAME, AVG(COLUMN2_NAME) FROM TABLE_NAME
GROUP BY COLUMN_NAME;
INSC 20263 (BIS)
October 4, 2020 13
Terry Gray
SQL
Keyword Summary
JOIN – joins two tables together based on primary key values equaling foreign key values
SELECT ORDERS.ORDERID, CUSTOMERS.CUSTOMERNAME, ORDERS.ORDERDATE
FROM ORDERS JOIN CUSTOMERS ON ORDERS.CUSTOMERID=CUSTOMERS.CUSTOMERID;
Table 1 Foreign Key Primary Key
joined with Column Column
Table 2
Table_name.column_name notation is required because multiple tables
are referenced and some tables may have columns with the same name
- This method tells SQL specifically which table the column is in
INSC 20263 (BIS)
October 4, 2020 14
Terry Gray
SQL
JOIN Patrons
PID Name Address
Transactions P1 J. Smith 12 Elk St
P2 M. Jones 25 Sun Dr
TID Borrow Due Return Book Patron
P1 J. Smith 12 Elk St
1 08/23/17 09/06/17 08/24/17 1001.1 P1
P4 V. Hicks 22 Dog St
2 08/23/17 09/06/17 09/13/17 2002.2 P2
P5 E. Rice 6 Bear Av
3 08/25/17 09/08/17 09/01/17 3003.3 P3
4 08/26/17 09/09/17 09/08/17 1001.1 P4
5 09/01/17 09/15/17 4004.4 P5 Books
6 09/05/17 09/19/17 09/17/17 3003.3 P2 BID Title Author
1001.1 Peace Ed Bart
2002.2 War Ann Hine
3003.3 Systems Al Vang
4004.4 Spring Sue Lyon
INSC 20263 (BIS)
October 4, 2020 15
Terry Gray
SQL
JOIN
Temporary
Table
Created
SELECT Transactions.Due Patrons.Name FROM Transactions
JOIN Patrons ON Transactions.Patron = Patrons.PID
TID Borrow Due Return Book Patron PID Name Address
1 08/23/17 09/06/17 08/24/17 1001.1 P1 P1 J. Smith 12 Elk St.
2 08/23/17 09/06/17 09/13/17 2002.2 P2 P2 M. Jones 25 Sun D
3 08/25/17 09/08/17 09/01/17 3003.3 P3
4 08/26/17 09/09/17 09/08/17 1001.1 P4 P4 V. Hicks 22 Dog S
5 09/01/17 09/15/17 4004.4 P5 P5 E. Rice 6 Bear A
6 09/05/17 09/19/17 09/17/17 3003.3 P2 P2 M. Jones 25 Sun D
INSC 20263 (BIS)
October 4, 2020 16
Terry Gray
SQL
JOIN
SELECT Transactions.Due Patrons.Name FROM Transactions
JOIN Patrons ON Transactions.Patron = Patrons.PID
JOIN Books ON Transactions.Book = Books.BID
INSC 20263 (BIS)
October 4, 2020 17
Terry Gray
SQL
SELECT Transactions.Due Patrons.Name
FROM Transactions Temporary
JOIN Patrons ON Transactions.Patron = Patrons.PID Table
JOIN Books ON Transactions.Book = Books.BID Created
TID Borrow Due Return Book Patro PID Name Address BID Title Author
n
1 08/23/17 09/06/17 08/24/17 1001.1 P1 P1 J. Smith 12 Elk St. 1001.1 Peace Ed Bart
2 08/23/17 09/06/17 09/13/17 2002.2 P2 P2 M. Jones 25 Sun D 2002.2 War Ann Hine
3 08/25/17 09/08/17 09/01/17 3003.3 P3 3003.3 Systems AI Vang
4 08/26/17 09/09/17 09/08/17 1001.1 P4 P4 V. Hicks 22 Dog S 1001.1 Peace Ed Bart
5 09/01/17 09/15/17 4004.4 P5 P5 E. Rice 6 Bear A 4004.4 Spring Sue Lyon
6 09/05/17 09/19/17 09/17/17 3003.3 P2 P2 M. Jones 25 Sun D 3003.3 Systems AI Vang
INSC 20263 (BIS)
October 4, 2020 18
Terry Gray
SQL
References
https://www.codecademy.com/articles/sql-commands
https://www.w3schools.com/sql/
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html#GUI
D-A3C0D836-BADB-44E5-A5D4-265BA5968483
Editors:
https://livesql.oracle.com/
https://sqliteonline.com/
INSC 20263 (BIS)
October 4, 2020 19
Terry Gray