Download presentation
Presentation is loading. Please wait.
Published byChester Martin Modified over 9 years ago
1
CS 3630 Database Design and Implementation
2
SQL Query Clause 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
3
Oracle Command Column (Col) Column Hotel_no format A9 Col Hotel_No format a9 heading 'Hotel No' -- This also work col Hotel_no format a9 heading "Hotel No" -- to get leading decimal digits Column Price format $999.99 heading 'Price/Night' -- to get leading 0s Column 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
4
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
5
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
6
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
7
Operator Between Select * From Room Where Price between 30 and 45; -- Operator Between -- inclusive Select * From Room Where Price Not between 30 and 45; -- Where Not Price between 30 and 45; 7
8
Operator IN Select * From Room Where Price in (30, 40, 45); -- Same as -- Where Price = 30 or Price = 40 or Price = 45 Select * From Room Where Price Not in (30, 40, 45); -- Same as -- Where Not Price in (30, 40, 45); 8
9
Comparing Strings Operator Like and % Select * From guest Where guest_name = 'Mary Tregear' Where guest_name like 'M%'; -- All guests with name beginning with 'M‘ -- %: any string of zero or more chars 9
10
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
11
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
12
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
13
Sorting the Result Select * From Room Order by Price desc; -- ASC is the default 13
14
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
15
Sorting the Result Select Hotel_No, Room_No, RType From Room Order by Hotel_No, Price desc; -- Price may not be selected 15
16
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
17
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
18
Character Functions Select Guest_Name || ' From ' || Address From Guest Where Address is not null; 18
19
Character Functions Lower Upper Initcap length replace -- Replace all occurrences substr Instr 19
20
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
21
Assignment 7 Due Today By 5 PM 21
22
Assignment 8 Due Wednesday, April 8 Formatting Style Sample SQL Script File 22
23
Project PhaseOne: Due Monday 23
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.