SQL – Simple Queries and JOIN MGMT 360 Database Management.

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

Query Formulation with SQL
Introduction to Structured Query Language (SQL)
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9 Advanced Query Formulation with SQL.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 3 The Relational Data Model.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 9 Joining Data from Multiple Tables
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Copyright © 2011 by Michael V. Mannino All rights reserved. Database Design, Application Development, and Administration, 5 th Edition Chapter 3 The Relational.
Chapter 2 The Relational Data Model. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Relational model basics Integrity.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Chapter 10 Application Development with Views. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Creating.
Chapter 3 Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Getting started.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Advanced Query Formulation with SQL. 9-2 Outline  Outer join problems  Type I nested queries  Type II nested queries and difference problems  Nested.
Chapter 9 Advanced Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Outer join problems.
9 Advanced Query Formulation with SQL (Chapter 9).
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Query Formulation with SQL.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
1 Welcome to the World of Databases! Database technology: crucial to the operation and management of modern organisations Major transformation in computing.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 10 Application Development with Views.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Lecture3b - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g ITBIS373 Database Development.
Chapter 3 Query Formulation with SQL. Outline Background Getting started Joining tables Summarizing tables Reasoning tools Advanced problems Data manipulation.
Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Chapter 2 The Relational Data Model. Outline Relational model basics Integrity rules Rules about referenced rows Relational Algebra.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Structured Query Language
Application Development with Views
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Basic SQL SELECT Statements
The Relational Data Model
Basic select statement
Query Formulation with SQL
Writing Basic SQL SELECT Statements
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Section 4 - Sorting/Functions
Presentation transcript:

SQL – Simple Queries and JOIN MGMT 360 Database Management

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;

Selecting Data From Database Choosing Specific Columns: SELECT column_name [, column_name] … FROM table_name; SELECT FacFirstName, FacLastName, FacSalary FROM Faculty;

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

Selecting Data From Database Using Expressions: SELECT FacFirstName, FacLastName, FacCity, FacSalary*1.1 AS IncreasedSalary, FacHireDate FROM Faculty; Precedence Hierarchy for Arithmetic Operators Parentheses Multiplication Division Subtraction Addition

Selecting Data From Database Comparison Operators: =,, =, <> SELECT OfferNo, CourseNo, FacSSN FROM Offering WHERE OffTerm = ‘SPRING’; SELECT OfferNo, CourseNo, FacSSN FROM Offering WHERE OffTerm <> ‘FALL’;

Selecting Data From Database SELECT FacFirstName, FacLastName, FacSSN FROM Faculty WHERE 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

Selecting Data From Database Boolean OR LOGICAL OPERATORS AND, OR, and NOT: SELECT FacFirstName, FacLastName, FacSalary FROM Faculty WHERE FacSalary > AND FacRank = 'PROF‘; SELECT OfferNo, CourseNo, FacSSN FROM Offering WHERE (OffTerm = 'FALL' AND OffYear = 1999) OR (OffTerm = 'WINTER' AND OffYear = 2000)

Logical Operator Precedence Parentheses Multiplication Division Subtraction Addition NOTANDOR 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’)) >= 1994 AND to_number(to_char(FacHireDate, ‘YYYY’)) <= 1995

Selecting Data From Database Lists Using IN and NOT IN: SELECT StdFirstName, StdLastNAme, StdMajor FROM Student WHERE StdClass IN ('JR', 'SR'); Using IS NULL, NOT NULL: SELECT OfferNo, CourseNo FROM Offering WHERE FacSSN IS NULL;

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 WHERE CourseNo LIKE ‘IS_2%’;

Eliminating Duplicate Rows Eliminating 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

Eliminating Duplicate Rows SELECT DISTINCT CourseNo from Offering; SELECT ALL CourseNo from Offering; (Same as the next statement) SELECT CourseNo from Offering;

Sorting Data Use of ORDER BY clause to order/sort the rows from a select query SELECT select_list FROM table_list [WHERE conditions] [ORDER BY {expression [ASC | DESC] | position [ASC | DESC]} [, {expression [ASC | DESC] | position [ASC | DESC] }] …]

Sorting Data Most 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

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;

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, StdMajor FROM Student WHERE StdClass IN ('JR', 'SR') ORDER BY 3 ASC, 1 DESC;

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.

Aggregate Functions Aggregate Function Result 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

Aggregate Functions and Grouping Data SELECT FacRank, AVG(FacSalary) AS AvgSalary FROM Faculty GROUP BY FacRank; SELECT FacRank, Max(FacSalary) AS Maximum_Salary, Min(FacSalary) AS Minimum_Salary FROM Faculty GROUP BY FacRank; SELECT CourseNo, Count(DISTINCT FacSSN) AS No_Insructors FROM Offering GROUP BY CourseNo;

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 AvgGpa FROM Student WHERE StdClass IN ('JR', 'SR') GROUP BY StdMajor HAVING AVG(StdGPA) > 3.1;

Grouping Data Overview SQL statement processing order (adapted from van der Lans, p.100)

Joining Tables Most 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

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

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

Joining Tables

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;

Joining Tables: Outer Join One-Sided Outer joins (using a + sign): SELECT OfferNo, CourseNo, FacLastName, Faculty.FacSSN FROM Offering, Faculty WHERE Faculty.FacSSN (+) = Offering.FacSSN; (Starts with all the rows from the Offering Table) SELECT OfferNo, CourseNo, FacLastName, Faculty.FacSSN FROM Offering, Faculty WHERE Faculty.FacSSN = Offering.FacSSN (+); (Starts with all the rows from the Faculty Table)

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

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

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