CS 3630 Database Design and Implementation

Slides:



Advertisements
Similar presentations
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
Advertisements

Chapter 4 Hotel (hotelno, hotelname, city)
Oracle 10g Express. Download Oracle 10g Express Oracle 10g Express Edition: – edition/overview/index.htmlhttp://
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
AGGREGATE FUNCTIONS Prof. Sin-Min Lee Surya Bhagvat CS 157A – Fall 2005.
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Assignment6-1 Assignment6-2 Due Wednesday, March 13 1.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Chapter 6 SQL Homework.
Chapter 5 SQL Homework.
Project Phase I Phase II Due Monday, April 15 Groups 1.
Mary K. Olson PS Reporting Instance – Query Tool 101.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
SQL – Logical Operators and aggregation Chapter 3.2 V3.0 Napier University Dr Gordon Russell.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CS 3630 Database Design and Implementation. Assignment 3 Style! Agreement between database designer and the client. UserName1_EasyDrive UserName2_EasyDrive.
SQL queries ordering and grouping and joins
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
Chapter 5 SQL: Data Manipulation Thomas Connolly, Carolyn Begg, Database System, A Practical Approach to Design Implementation and Management, 4 th Edition,
SQL introduction 2013.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
CS 3630 Database Design and Implementation. Null Value The value of an attribute could be NULL NOT known at the moment or NOT Applicable Example Cell.
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
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.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
CS 3630 Database Design and Implementation. Joins Retrieve data from two or more tables Join Conditions PK and FK (Natural Join) Other attributes (Theta.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
How to: SQL By: Sam Loch.
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
Assignment 2 Relational Algebra Which tables? What operations?
CS3220 Web and Internet Programming More SQL
In this session, you will learn to:
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CPSC-310 Database Systems
Latihan Answer the following questions using the relational schema from the Exercises at the end of Chapter 3: Create the Hotel table using the integrity.
The Database Exercises Fall, 2009.
CS 440 Database Management Systems
Assignment 2.
CS 3630 Database Design and Implementation
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
SQL – Entire Select.
Built in Functions Massaging the data.
Chapter 4 Summary Query.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Access Tutorial 5 Creating Advanced Queries and Enhancing Table Design
Creating Noninput Items
COP 2700 – Data Structures - SQL
Query Functions.
Section 4 - Sorting/Functions
LINQ to SQL Part 3.
Presentation transcript:

CS 3630 Database Design and Implementation

Due Wednesday, April 4, by 5 PM Class dropbox on K: drive Project Phase II Due Wednesday, April 4, by 5 PM Class dropbox on K: drive

Assignment 8 Due Friday, April 6, by 5 PM

Aggregate Functions: Produce a single value from a set Count Max Min Sum Avg …

SQL Aggregate Functions -- Max, Min, Avg, Sum Select Max(price), Min(price), Avg(price) From room;

Formatting -- Formatting Col MaxPrice format $99.00 heading "Max Price" Select Max(price) MaxPrice, Min(price) MinPrice, Avg(price) AvgPrice From room;

Formatting -- Formatting Col MaxPrice format $999.00 heading "Max Price" Col MinPrice format $999.00 heading "Min Price" Col AvgPrice format $999.00 heading "Avg Price" Select Max(price) MaxPrice, Min(price) MinPrice, Avg(price) AvgPrice From room;

SQL Aggregate Functions -- Room_No char(4) Select Max(room_no), Min(room_no) From room;

SQL Aggregate Functions // Room_No char(4) // No average or sum on strings! Select Avg(room_no), Sum(room_no) From room;

Max/Min Dates Col Min(Date_From) Format a20 Heading 'Earlist Date_From' Col Max(Date_To) Format a20 Heading 'Latest Date_To' Select Min(Date_From), Max(Date_To) From Booking;

Max/Min Dates col "Earliest Date from" format a20 col "Latest Date To" format a20 Select To_Char(Min(Date_From), 'Mon dd yyyy') "Earliest Date from", To_Char(Max(Date_To), 'Mon dd yyyy') "Latest Date To" From Booking;

Incorrect! col "Earliest Date from" format a20 col "Latest Date To" format a20 Select Min(To_Char(Date_From, 'Mon dd yyyy')) "Earliest Date from", Max(To_Char(Date_To, 'Mon dd yyyy')) "Latest Date To" From Booking;

Count -- Count(*): number of all records -- count(hotel_no): number of Hotel_No -- Count(unique Hotel_No): -- number of distinct Hotel_No Select count(*), count(hotel_no), count(unique hotel_no) From room; Pause Select * From room Order by hotel_no;

Count -- Does not count null values select count(*), count(address) From guest; Select * From guest;

Aggregate Functions with Distinct/Unique Select Avg(Price) From room; -- With Distinct Select Avg(Distinct Price) Select Avg(Unique Price) Select * From room Order by price;

Aggregate Functions with Where -- With where clause -- Avg is applied after Where clause col "Average Price" format $999.00 Select Avg(Price) "Average Price" From room Where rtype = 'Family'; Select Avg(Price) "Average Price" Where rtype = 'Single'; Where rtype = 'Double';

Aggregate Functions with Where -- With where clause -- Avg is applied after Where clause Select 'Average Price of Family rooms: ', Avg(Price) From room Where rtype = 'Family'; Pause Prompt Run time error! Select rtype, Avg(Price)

One record for each group. Group By Clause Count, Avg Select Group By Count, Avg Count, Avg One record for each group.

Group By Select Avg(Price) From room Group by rtype; Select rtype, Avg(Price)

Group By Clause For each hotel, display Hotel_No with the number of rooms. Select Hotel_No, Count(*) From Room Group By Hotel_No; Hotel_No Room_No Price H01 R001 30 R002 35 H05 R003 40 R103 45 R101 50 Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Count H01 3 H05 2

What can be selected with Group By Clause Can select Hotel_No, the group by field Can select aggregate functions on any other fields such as Max(Room_No) or Avg(Price) Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Max(Room_No) Avg(Price) H01 R103 36.67 H05 R101 45

What CANNOT be selected with Group By Clause Cannot select other fields (without aggregate function) not in the group by clause ORA-00979: not a GROUP BY expression Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Room_No Price H01 ? H05

Group By Clause ? Select Group By ? ? One record for each group! What can be selected? Aggregate functions Group by fields

One Record for Each Group Select Hotel_no, Count(*), Max(Price) From Room Group by Hotel_No; Select Hotel_no, Room_No, rtype, Price -- not a Group By expression

Group by More Than One Field Select Hotel_No, rtype, Max(Price) From Room Group By Hotel_no, rtype Order By Hotel_no, rtype

Order After Group by Select Hotel_no, Count(*), Max(Price) From Room Group by Hotel_No Order by Count(*);

Group By Clause ? Select Group By ? ? What if we don’t want all groups? Having on groups

Having to select groups Select Hotel_no, Count(*), Max(Price) From Room Group by Hotel_No; Pause Group by Hotel_No Having Count(*) >= 5;

Group By Where before Group by to select records Having after Group by to select groups Select Hotel_no, Count(*), Max(Price) From Room Where rtype = 'Single' Group by Hotel_No Having Max(Price) < 100;

Where before Group by to select records Having after Group by From Room Order by rtype; pause Where rtype = 'Single'; Select Hotel_no, Count(*), Max(Price) Where rtype = 'Single' Group by Hotel_No; Pause Group by Hotel_No Having Max(Price) < 100; Where before Group by to select records Having after Group by to select groups Let’s do it in 4 steps: From Where Group By Having

Where before Group by Having after Group by Order by last Select Hotel_no, Count(*), Max(Price) From Room Where rtype in ('Double', 'Single') Group by Hotel_No Order by Count(*); pause Having Count(*) > 3 Order by Count(*) desc;

Group By Clause Execution Order Where (condition on records) Group by Having (condition on groups) Order by (Select)

Script File Style Prompt Prompt 9. Prompt For each hotel that has more than Prompt one Single room, list hotel number Prompt with number of Single rooms the hotel Prompt has and the highest price of Prompt such rooms of the hotel. Col Count(*) Format 99 heading "Single Rooms" Col Max(Price) format $999.00 heading "Max Price" Select Hotel_no, Count(*), Max(Price) From Room Where rtype = 'Single' Group by Hotel_No Having Count(*) > 1 Order by Count(*); pause

Assignment 8 Due Friday, April 6 Should work for any table instance Run against my tables Should insert more records to test (Don’t include insert commands!) Example Quiz 3 Hands-On Wednesday, April 7 Create Script file to Drop tables Create tables Column/Table constraints Insert records

Quiz 3 Wednesday, April 11 Run Script file to Drop/Create tables, insert records Create Script file to query database Hands-on: in Lab 206 D2L