You are on page 1of 4

--LAB3--

select * from propertyforrent;


-- l. List full details of all properties.

select * from propertyforrent


where type='House';
-- 2. List full details of all properties that are only house type.

select * from client;


select clientno as "client number",
fname as "first name",
lname as "last name",
* all
telno as "telephone number"
distinct (difference) ไม่เอาทีซ่ ้า
from client
where preftype='Flat';
-- 3. List the client number, first name, last name and telephone number of all clients whose preferred
property type are Flat.

select * from viewing;


select clientno, viewdate
from viewing
where propertyno = 'PG21'
and comments is not null;
-- 4. List the client number and view date of all viewings on property PG21 where the comments are
provided.

select distinct type from propertyforrent;


-- 5. List all type of properties that are for rent. The result should eliminate the duplicate
records.

select * from staff;


select staffno as "staff number",
fname as "first name",
lname as "last name",
salary as "salary"
from staff where sex='M' and salary > 10000 ;
-- 6. List the staff number, first name, last name and salary of all staffs who are males with salary greater
than 10,000.
select * from staff;
select staffno as "staff number",
fname as "first name",
lname as "last name",
dob as "birth date"
from staff
where dob > '1-JAN-1960'
order by dob;
-- 7. List the staff number, first name, last name and birth date of all staffs who were born after January
1, 1960. Arrange the result in ascending order by birth date.

select * from privateowner;


select ownerno as "owner number",
fname||lname as "name",
address as "address"
from privateowner
where address not like '%Glasgow%';
-- 8. List the owner number, name (first name concatenated with last name), and address of all private
owners who do not live in the Glasgow city.

select * from branch;


select street,city,postcode
from branch
where city in ('Aberdeen','Bristol');
-- 9. List the address of all branch offices in Aberdeen or Bristol.

select * from staff; 2 types of ORDER BY


select branchno as "branch no", asc : ascending เพิม
่ ขึ ้น จากน้ อยไปมาก (default)
salary as "salary" desc : descending ลดลง จากมากไปน้ อย
from staff
order by branchno, salary desc;
-- 10. List the branch no and salary of all staffs. The result should be arranged by branch no, in ascending
order, and within branch no, in descending order of salary.

--LAB4-- LIKE ‘_____’


select * from branch;
select branchno as "branch number" _ denotes 1 character
from branch % denotes 0 or many characters
where city in ('Aberdeen')
or street like '%Main Street%';
-- 1. Show the branch number of all branches that locate in Aberdeen city or on Main street.
select * from branch where city <> 'London';
-- 2. List the branches that are not in London // != or <> or not like

select * from client where preftype = 'Flat' and maxrent <=400;


-- 3. Show clients, whose preferred property type are Flat, can rent the property maximum 400.

select * from client;


select * from client where telno like '%77';
-- 4. Show clients with phone number ending in 77.

select * from propertyforrent;


select propertyno as "property number", rent as "rent cost"
from propertyforrent
where type = 'Flat' and city = 'Glasgow' and rooms between 3 and 4; --in (3,4);
-- 5. Show property number and the rent cost of the flat properties in Glasgow city with at least 3 and up
to 4 rooms.

select propertyno from propertyforrent


where ownerno like 'CO87' or ownerno like 'CO40';
-- 6. Display the property number of properties with owner number is CO87 or owner number is CO40.

select * from propertyforrent where staffno is null;


-- 7. Show properties that do not have staff number provide. --is null

select * from staff where position = 'Manager' order by salary desc;


-- 8. Show the staffs with the position Manager and sort the staffs’ information in descending order of
salary.

select * from staff;


select staffno,position,dob from staff
where dob like ('%JUN%')
order by dob desc;
-- 9. Show staff number, position and day of birth of staffs who have birthdate in June. The result should
be arranged by birthdate in descending order.
SELECT [DISTINCT|ALL]{*|column|expression [[AS] alias][,...]}
FROM table [alias][,...]
[WHERE condition(s)]
[GROUP BY columnList][HAVING condition]
[ORDER BY {column|expr|alias} [ASC|DESC][,...]] ;

Description Operator
Add +
Subtract -
Multiply *
Divide /

Meaning Operator
Equal to =
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=
Not equal to <> , !=
Between two values (inclusive) BETWEEN...AND...
Match any of a list of values IN ( set )
Is a null value IS [NOT] NULL
Match a character pattern LIKE

Writing SQL Statements


- SQL statements are not case-sensitive.
- SQL statements can be on one or more lines.
- Keywords cannot be abbreviated or split across lines.
- Clauses are usually placed on separate lines.
- You are required to end each SQL statement with a semicolon (;).

You might also like