CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking;...
-
Upload
chester-martin -
Category
Documents
-
view
226 -
download
0
Transcript of CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking;...
CS 3630 Database Design and Implementation
SQL QueryClause Select and From
Select *
From booking;
select hotel_no, guest_no, room_no
from booking;
select Distinct hotel_no, guest_no, room_no
from booking;
-- Remove duplicate records
-- Unique also works
2
Oracle Command Column (Col)Column Hotel_no format A9
Col Hotel_No format a9 heading 'Hotel No'
-- This also workcol Hotel_no format a9 heading "Hotel No"
-- to get leading decimal digitsColumn Price format $999.99 heading 'Price/Night'
-- to get leading 0sColumn Price format $099.99 heading 'Price/Night'
Col Date_To format a16 heading 'Date of Leaving' Col Date_From format a16 heading 'Date of|Arrival'
Select * from Room;
Select * from Booking;
Clear col 3
Clause Where
Select *
From Room
Where RType = 'Single‘;
-- Where clause to specify the condition
-- Operators And/Or
Select *
From Room
Where Price > 35 and RType = 'Single‘;
-- Where Price > 35 or RType = 'Single'
4
Operator NOT (!)
Select *
From Room
Where Price > 35 and RType != 'Family';
-- Where Price > 35 and Not (RType = 'Family‘)
-- Where Price > 35 and NOT RType = 'Family‘
-- Not working:
-- Where Price > 35 and !(RType = 'Family‘)
5
Operator NOT (!)
Select *
From Room
Where Not (Price > 35 and RType = 'Family');
-- Not the same!
Select *
From Room
Where Not Price > 35 and RType = 'Family';
--Where (Not Price > 35) and RType = 'Family';
--Where Price <= 35 and RType = 'Family';
6
Operator Between
Select *From RoomWhere Price between 30 and 45;
-- Operator Between-- inclusive
Select *From RoomWhere Price Not between 30 and 45;-- Where Not Price between 30 and 45;
7
Operator IN
Select *From RoomWhere Price in (30, 40, 45);-- Same as-- Where Price = 30 or Price = 40 or Price = 45
Select *From RoomWhere Price Not in (30, 40, 45);-- Same as-- Where Not Price in (30, 40, 45);
8
Comparing StringsOperator Like and %
Select *From guestWhere guest_name = 'Mary Tregear'Where guest_name like 'M%';-- All guests with name beginning with 'M‘-- %: any string of zero or more chars
9
Operator Like and Char ‘_’
-- All records whose Fname has 'M' for 3rd position
Select *
From Guest
Where guest_name like '__M%';
-- _: any single char
10
Null Value
Select *
From Guest
Where Address = null;
-- No row is selected
-- null != null
Select *
From Guest
Where Address is null;
-- Some rows may be selected
11
Changing Headings-- use " " for heading
-- Use ' ' for string
-- With or without "AS"
Select Guest_no, Guest_name as "Guest Name"
From Guest;
Select Guest_no, Guest_name "Guest Name"
From Guest;
Select Guest_no, Guest_name as GuestName
From Guest;
Select Guest_no, Guest_name GuestName
From Guest;12
Sorting the Result
Select *
From Room
Order by Price desc;
-- ASC is the default
13
Sorting the Result
Select *
From Room
Order by Hotel_No, Price desc;
-- Sorting first on Hotel_No (asc),
-- then on Price (desc)
-- ASC is the default
14
Sorting the Result
Select Hotel_No, Room_No, RType
From Room
Order by Hotel_No, Price desc;
-- Price may not be selected
15
Sorting the Result
-- Can be used in Order by
Select Guest_no, Guest_name as GuestName
From Guest
Order by GuestName;
Select Guest_no, Guest_name as "Guest Name" From Guest Order by "Guest Name“;
16
Numeric Operators/Functions
Select Hotel_No, Room_No, Price * 0.90 "Discount Price"
From Room;
-- Where Hotel_No = ‘H01';
-- operators: +, -, /, *
-- functions: ceil, floor, mod, power, sign, sqrt
-- With As
17
Character Functions
Select Guest_Name || ' From ' || Address
From Guest
Where Address is not null;
18
Character Functions
Lower
Upper
Initcap
length
replace
-- Replace all occurrences
substr
Instr
19
Character Functions
Select substr(Guest_name, 1, 9)From Guest;-- substr(Guest_name, 1, 9) up to 9 characters
Select * From Guest where instr(Guest_name, 'T', 6) > 0;
Select * From Guest where instr(Guest_name, 'T', 7) > 0;
20
Assignment 7
Due Today By 5 PM
21
Assignment 8
Due Wednesday, April 8
Formatting
Style
Sample SQL Script File
22
Project
PhaseOne: Due Monday
23