sql – simple queries and join mgmt 360 database management
TRANSCRIPT
![Page 1: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/1.jpg)
SQL – Simple Queries and JOIN
MGMT 360MGMT 360
Database ManagementDatabase Management
![Page 2: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/2.jpg)
Selecting Data From Database
General Form:
SELECT list_of_columns FROM table[s] [WHERE search_conditions]
Choosing all columns:SELECT * FROM table_list;
Examples: SELECT * FROM FACULTY; SELECT * FROM STUDENT;
![Page 3: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/3.jpg)
Selecting Data From Database
Choosing Specific Columns:
SELECT column_name [, column_name] … FROM table_name;
SELECT FacFirstName, FacLastName, FacSalaryFROM Faculty;
![Page 4: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/4.jpg)
Selecting Data From Database
Renaming Columns:
SELECT column_name as column_heading
FROM table_name;
SELECT FacFirstName AS First, FacLastName AS Last, FacSalary AS Salary
FROM Faculty
![Page 5: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/5.jpg)
Selecting Data From Database
Using Expressions:
SELECT FacFirstName, FacLastName, FacCity, FacSalary*1.1 AS IncreasedSalary,
FacHireDate
FROM Faculty;Precedence Hierarchy for Precedence Hierarchy for
Arithmetic OperatorsArithmetic Operators
Parentheses
Multiplication Division
Subtraction Addition
![Page 6: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/6.jpg)
Selecting Data From Database
Comparison Operators:= , <, >, <=, >=, <>
SELECT OfferNo, CourseNo, FacSSN FROM Offering WHERE OffTerm = ‘SPRING’;
SELECT OfferNo, CourseNo, FacSSN FROM OfferingWHERE OffTerm <> ‘FALL’;
![Page 7: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/7.jpg)
Selecting Data From Database
SELECT FacFirstName, FacLastName, FacSSN FROM FacultyWHERE to_number(to_char(FacHireDate, ‘YYYY’)) > 1991;
SELECT FacFirstName, FacLastName, FacCity, FacSalary*1.1 AS IncreasedSalary, FacHireDate FROM Faculty
WHERE to_number(to_char(FacHireDate, 'YYYY')) > 1991
![Page 8: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/8.jpg)
Selecting Data From Database
Boolean OR LOGICAL OPERATORS AND, OR, and NOT:
SELECT FacFirstName, FacLastName, FacSalaryFROM FacultyWHERE FacSalary > 65000 AND FacRank = 'PROF‘;
SELECT OfferNo, CourseNo, FacSSN FROM Offering WHERE (OffTerm = 'FALL' AND OffYear = 1999) OR (OffTerm = 'WINTER' AND OffYear = 2000)
![Page 9: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/9.jpg)
Logical Operator PrecedenceLogical Operator PrecedenceParenthesesParentheses
Multiplication DivisionMultiplication Division
Subtraction AdditionSubtraction Addition
NOTNOT
ANDAND
OROR
Selecting Data From Selecting Data From DatabaseDatabase
![Page 10: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/10.jpg)
Selecting Data From Database
Ranges: With the comparison operators > and < With the keyword BETWEEN
SELECT FacFirstName, FacLastName, FacHireDate FROM Faculty WHERE to_number(to_char(FacHireDate, ‘YYYY’))BETWEEN 1994 AND 1995
SELECT FacFirstName, FacLastName, FacHireDate FROM Faculty WHERE to_number(to_char(FacHireDate, ‘YYYY’)) >= 1994AND to_number(to_char(FacHireDate, ‘YYYY’)) <= 1995
![Page 11: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/11.jpg)
Selecting Data From Database
Lists Using IN and NOT IN:
SELECT StdFirstName, StdLastNAme, StdMajorFROM Student WHERE StdClass IN ('JR', 'SR');
Using IS NULL, NOT NULL:
SELECT OfferNo, CourseNo FROM Offering WHERE FacSSN IS NULL;
![Page 12: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/12.jpg)
Selecting Data From Database
Matching Character Strings Using LIKE: Two wildcard characters for use with LIKE:
% any string of zero or more characters - any single character (Used with %)
SELECT CourseNo FROM Offering WHERE CourseNo LIKE 'IS%‘;
SELECT CourseNo FROM Offering WHERECourseNo LIKE ‘IS_2%’;
![Page 13: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/13.jpg)
Eliminating Duplicate RowsEliminating Duplicate Rows: Distinct and All
DISTINCT returns only those rows that are unique
ALL returns all qualified rows, by default all rows are returned by a SELECT query
SELECT [DISTINCT | ALL] select_list
Distinct combination of items
DISTINCT treats each null in a particular column as a duplicate of all other null values in that column
![Page 14: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/14.jpg)
Eliminating Duplicate Rows
SELECT DISTINCT CourseNo from Offering;
SELECT ALL CourseNo from Offering; (Same as the next statement)
SELECT CourseNo from Offering;
![Page 15: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/15.jpg)
Sorting Data
Use of ORDER BY clause to order/sort the rows from a select query
SELECT select_listFROM table_list[WHERE conditions][ORDER BY {expression [ASC | DESC] | position[ASC | DESC]}[, {expression [ASC | DESC] | position [ASC | DESC] }] …]
![Page 16: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/16.jpg)
Sorting DataMost systems require that each sort element (column or expression) appear in the select list
If multiple columns are specified the sort is done based on the order in which columns are specified
Default sort direction is ASC
NULL will appear at the beginning or the end of the sorted list
![Page 17: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/17.jpg)
Sorting Data
SELECT StdFirstName, StdLastName, StdCity
FROM Student
ORDER BY StdLastName;
SELECT StdClass, StdFirstName, StdLastNAme, StdMajor
FROM Student
WHERE StdClass IN ('JR', 'SR')
ORDER BY StdClass ASC, StdLastName DESC;
![Page 18: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/18.jpg)
Sorting Data
Three options for expressions: Use an integer to represent the expression’s position
in the select list Use a column heading in the select list Use the whole expression
SELECT StdClass, StdFirstName, StdLastNAme, StdMajorFROM Student WHERE StdClass IN ('JR', 'SR')ORDER BY 3 ASC, 1 DESC;
![Page 19: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/19.jpg)
Aggregate Functions
Used to obtain summary values
Aggregate functions always take an argument
General syntax of aggregate functions: aggregate_function ([DISTINCT] expressions)
(You can omit the DISTICT clause)
Aggregate functions are used in conjunction with GROUP BY clause in a SELECT query.
![Page 20: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/20.jpg)
Aggregate Functions
Aggregate FunctionAggregate Function ResultResult
SUM ([DISTINCT] expression) The total of (distinct) values in the numerical expression
AVG([DISTINCT] expression) The average of (distinct) values in the numeric expression
COUNT([DISTINCT] expression) The number of (distinct) non-null values in the expression
COUNT(*) The number of selected rows
MAX(expression) The highest value in the expression
MIN(expression) The lowest value in the expression
![Page 21: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/21.jpg)
Aggregate Functions and Grouping Data
SELECT FacRank, AVG(FacSalary) AS AvgSalaryFROM Faculty GROUP BY FacRank;
SELECT FacRank, Max(FacSalary) AS Maximum_Salary,Min(FacSalary) AS Minimum_SalaryFROM Faculty GROUP BY FacRank;
SELECT CourseNo, Count(DISTINCT FacSSN) AS No_Insructors FROM Offering GROUP BY CourseNo;
![Page 22: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/22.jpg)
Conditions on Grouped Data
Use HAVING to select row that satisfy some condition on grouped data
You can still use WHERE clause to select rows according to some condition WHERE eliminates the rows first and then groups the data HAVING groups first and then eliminates the rows
SELECT StdMajor, AVG(StdGPA) AS AvgGpaFROM Student WHERE StdClass IN ('JR', 'SR')GROUP BY StdMajorHAVING AVG(StdGPA) > 3.1;
![Page 23: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/23.jpg)
Grouping Data Overview
SQL statement processing order SQL statement processing order (adapted from van der Lans, (adapted from van der Lans, p.100)p.100)
![Page 24: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/24.jpg)
Joining TablesMost databases have many tables
Combine tables using the join operator (Inner Join or Outer Join)
Specify matching condition Can be any comparison but usually = PK = FK most common join condition Relationship diagram useful when combining tables Columns being compared should have similar values Join column data types must be compatible Use alias names to improve readability
![Page 25: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/25.jpg)
Joining Tables
Join Types: Equijoin Natural join Self join Outer join
Equijoin: Joining condition is based on equality between
values in the common column Common columns appear (repeat) in the result table
Inner Joins
![Page 26: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/26.jpg)
Joining Tables
Natural Join: Same as equijoin except join column displayed only
once Natural Join may be based on other relational
operators
Self-join: Compares values within a column of a single table
Outer Joins: Rows that do not have matching values in common
columns are included in the result table
![Page 27: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/27.jpg)
Joining Tables
FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara
OfferNo FacSSN
1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
FacSSN FacName OfferNo 111-11-1111 joe 1111
222-22-2222 sue 2222
111-11-1111 joe 3333
Faculty
Offering
Natural Join of Offering andFaculty
![Page 28: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/28.jpg)
Joining Tables: Inner join
List Tables in the FROM clause
List conditions in the WHERE clause
SELECT OfferNo, CourseNo, FacFirstName, FacLastName FROM Offering, Faculty WHERE OffTerm = 'FALL' AND OffYear = 1999 AND FacRank = 'ASST' AND CourseNo LIKE 'IS%'AND Faculty.FacSSN = Offering.FacSSN;
![Page 29: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/29.jpg)
Joining Tables: Outer Join
One-Sided Outer joins (using a + sign):
SELECT OfferNo, CourseNo, FacLastName, Faculty.FacSSNFROM Offering, Faculty WHERE Faculty.FacSSN (+) = Offering.FacSSN;
(Starts with all the rows from the Offering Table)
SELECT OfferNo, CourseNo, FacLastName, Faculty.FacSSNFROM Offering, Faculty WHERE Faculty.FacSSN = Offering.FacSSN (+);
(Starts with all the rows from the Faculty Table)
![Page 30: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/30.jpg)
Joining Tables: Outer Join
Full Outer Join: SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty, Student WHERE Student.StdSSN = Faculty.FacSSN (+)
UNION SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty, Student WHERE Student.StdSSN (+) = Faculty.FacSSN
![Page 31: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/31.jpg)
Joining Tables: Outer Join
Mixing Inner and Outer Joins: SELECT OfferNo, Offering.CourseNo, OffTerm, CrsDesc, Faculty.FacSSN, FacLastName FROM Faculty, Course, Offering WHERE Offering.FacSSN = Faculty.FacSSN (+) AND Course.CourseNo = Offering.CourseNo AND Course.CourseNo LIKE 'IS%'
![Page 32: SQL – Simple Queries and JOIN MGMT 360 Database Management](https://reader036.vdocument.in/reader036/viewer/2022062320/56649f485503460f94c6a0c4/html5/thumbnails/32.jpg)
Functions
ORACLE has several functions that you can use:
Arithmetic functions: ABS, CEIL, FLOOR etc.String Manipulation Function: CONCAT, LENGTH, LPAD etc.Date Functions: SYSDATE, ADD_MONHTS, LAST_DATE etc.
More details can be found in your ORACLE Reference Book