BCI2023 Chapter7 SQL v1.2 PDF
BCI2023 Chapter7 SQL v1.2 PDF
Foreign key of
dependent table
Controlling Attributes Values
SQL Data Types
Alter Tables
• ALTER TABLE statement allows you to change
column specifications:
• Table Actions:
• SQL>ROLLBACK;
o Rollback complete.
o Reverses all changes to data made during your
session
• SQL>COMMIT;
o Makes all changes to this point permanent
o Points at which commit is issued, define extent of
rollback
o Rollback reverses every change since the last
commit
o Exiting sqlplus issues a commit
SQL for Retrieving Data from One Table
• ORDER BY Clause
• UNION, EXCEPT, INTERSECT
• IN
SQL for Retrieving Data from
Two or More Tables
SQL provides two ways to retrieve data from related tables:
• Join - When two or more tables are joined by a common
field.
• Subqueries - When one Select command is nested within
another command.
JOINS TABLES
SQL - Joins
• The WHERE clause is used to specify the common
field.
• For every relationship among the tables in the
FROM clause, you need one WHERE condition (2
tables - 1 join, 3 tables - 2 joins…)
SELECT C.Cust_ID, Comp_Name, Country,OrderID
FROM Customer AS C, Order AS O
WHERE C.Cust_ID = O.Cust_ID
AND Country = ‘USA’;
SQL - Joins
SECTION
INSTRUCTOR
TRANSCRIPT
STUDENT
STUDENT_NUMBER
STUDENT_NUMBER COURSE
COURSE_CODE
STUDENT_NAME GRADE
STUDENT_ADDRESS QUAR_YR COURSE_CODE
STUDENT_PHONE COURSE_NAME
MAJOR CREDIT_HOURS
SELECT Command
SELECT STUDENT.STUDENT_NUMBER,
STUDENT_NAME, TRANSCRIPT.COURSE_CODE,
GRADE, QUAR_YR, COURSE_NAME, CREDIT_HOURS
FROM STUDENT, TRANSCRIPT, COURSE
WHERE STUDENT.STUDENT_NUMBER
=TRANSCRIPT.STUDENT_NUMBER
AND TRANSCRIPT.COURSE_CODE
=COURSE.COURSE_CODE;
Three Tables
The number of joins is always
➔ Two Joins
one less than the number of
tables involved in the query
Results
STUD STUDENT_NAME COUR GR QUA COURSE_NAME CREDIT
1121 TRENT RAZEK MIS320 A 1/91 SYSTEMS I 4
1121 TRENT RAZEK MIS420 C 2/92 SYSTEMS II 4
1121 TRENT RAZEK MIS495 B 3/93 MGT INFO SYSTEMS 4
Notice how you get “logical” rows back from the select
as if they can from a single table when in fact the data
comes from three separate tables
Using Aliases
• Aliases for table names can be created in
the FROM part of the SELECT statement.
INSTRUCTOR SALARY
INSTRUCTO SUM(SALARY) 100000000 2500
--------- ----------- 100000000 3400
100000000 5900 200000000 3500
200000000 5900 200000000 2400
300000000 4500
300000000 4500
400000000 3400
400000000 4600 400000000 1200
500000000 1900 500000000 1900
GROUP BY and WHERE
SELECT INSTRUCTOR_ID, SUM(SALARY)
FROM STAFFING
WHERE SALARY>2500
GROUP BY INSTRUCTOR_ID;
INSTRUCTO CALL SALARY
INSTRUCTO SUM(SALARY) --------- ---- ----------
----------------- -------------------- 100000000 0001 2600
100000000 6000 100000000 0002 3400
200000000 6100
300000000 4500 200000000 0003 3500
400000000 3400 200000000 0004 2600
An intermediate
recordset is developed
after each clause.
Summary of Select
Statements
• SELECT - list of attributes and functions
• FROM - list of tables
• WHERE - conditions / join conditions
• GROUP BY - attributes not aggregated in select clause
• HAVING - group condition
• ORDER BY - list of attributes
• Subqueries (Nested queries)
– Example
– Correlated subquery
• Join types
– Inner/outer
• Integrity constraints
• Triggers
• Functions
Sample Database
• Scripts to create and populate the database are available on the 6217 Web
site.
SUPPLIERS
QUOTATIONS
PART_NO SUPPLIER_NO PRICE DELIVERY
INVENTORY
PART_NO Q_ON_ORDER Q_ON_HAND COMPONENT
SUBQUERIES
Subqueries
• A subquery is a query that is used in the
WHERE condition of another query
• AKA Nested query
• Can be multiple levels of nesting
• Can be used with SELECT, INSERT, UPDATE
Example 1: By “Hand”
**** LIST PARTS W/ > AVERAGE NUMBER OF
Q_ON_HAND
**** FIRST QUERY: DETERMINE AVERAGE
SELECT AVG(Q_ON_HAND)
FROM INVENTORY
;
SELECT *
FROM SUPPLIERS
WHERE NOT EXISTS
(SELECT *
FROM QUOTATIONS
WHERE SUPPLIER_NO =
SUPPLIERS.SUPPLIER_NO)
ORDER BY SUPPLIER_NO;
Correlated Subqueries
SELECT AVG(PRICE)
FROM QUOTATIONS
WHERE PART_NO = 321;
• ADD_MONTHS(d,n)
• LAST_DAY(d)
• MONTHS_BETWEEN(d1, d2)
• ROUND(d[,fmt])
• SYSDATE
• TO_CHAR(d [, fmt [, 'nlsparams'] ])
• TO_DATE(char [, fmt [, 'nlsparams'] ])
Data Dictionary Tables
• DICTIONARY
– All tables and views that are available to the user
– This table contains several hundred rows
• Useful Data Dictionary Views
– Use just like a table
– More useful (generally) than full tables
– Use DESCRIBE to see the columns in the view
– USER_TABLES
– USER_VIEWS
– USER_CONSTRAINTS
– USER_OBJECTS
Are you able to..