Вы находитесь на странице: 1из 4

2.2 Restrictions, ordering and limit.

1. Find the long name of all buildings with facility code ’T’ , ordered by area in descending
order (Result: 11 rows, beginning with “Mòdul Docent”)
SELECT * FROM buildings
WHERE facilityke = 'T'
ORDER BY bldgarea DESC;

2. Find the 3 buildings with the highest area (Result: 3 rows, codes HC, TD and JAA)
SELECT * FROM buildings
ORDER BY bldgarea DESC
LIMIT 3;

3. Select all buildings with an area between 5000 and 6000 m2, in descending order of
area. (Result: ITC, JC1, NA).
SELECT * FROM buildings
WHERE (bldgarea > 5000 and bldgarea < 6000)
ORDER BY bldgarea DESC;

4. Select all buildings that contain “Facultat” in their long name. Hint: use the like
operator (Chapter 9 of the Postgres documentation). (Result: DC, JC1, JAA, JC2, HC).
SELECT * FROM buildings
WHERE longname LIKE '%Facultat%'
ORDER BY longname ASC;

2.3 Functions and aggregation


1. What is the total number of rows in the buildings table? (Result: 44)
SELECT COUNT(*) FROM buildings;

2. What is the number of facility types that appear in the table? Hint: check in the
documentation the distinct modifier of count. (Result: 14)
SELECT COUNT(DISTINCT facilityke) FROM buildings;

3. What is the total number of buildings with facility type D? (Result: 5)


SELECT COUNT(*) FROM buildings WHERE facilityke = 'D';

4. What is the total area of all buildings (Result: 183307.94)


SELECT SUM(bldgarea) FROM buildings;

5. What is the average number of floors in all buildings? Round the number to the
nearest integer. And what if you only take into account buildings for which the
number of floors is actually recorded (that is, it is not 0)? Result: 3 and 4 respectively
SELECT ROUND(AVG(floorcount)) FROM buildings;
SELECT ROUND(AVG(floorcount)) FROM buildings WHERE floorcount <> 0;
2.4 Aggregation with grouping
1. Count the number of buildings that have each facility type.

SELECT facilityke, COUNT(*) FROM buildings GROUP BY facilityke;

2. Modify the previous query to include also the total area, and return the results
ordered by total area descending.

SELECT facilityke, COUNT(*), SUM(bldgarea)


FROM buildings
GROUP BY facilityke
ORDER BY SUM(bldgarea) DESC;

3. Find all facility types whose total area is greater than 25000 m2 (Result: T, D, J and H)
SELECT facilityke, COUNT(*), SUM(bldgarea)
FROM buildings
GROUP BY facilityke
HAVING SUM(bldgarea) > 25000 ;
4. (*) For each interval of 5000 m2 (0-5000, 5000-10000, etc.), count the number of
buildings with an area in that interval. Hint: You can group by arbitrary arithmetic
expressions; use trunc.

SELECT FLOOR(BLDGAREA/5000) *5000 AS FROM AREA, FLOOR(BLDGAREA/5000)


*5000+5000-1 AS TO AREA, COUNT (*) FROM BUILDINGS
GROUP BY FLOOR(BLDGAREA/5000)
ORDER BY FROM AREA ASC;

2.5 Simple subqueries


1. Show the area of the 5 largest buildings as a percentage of the total area. Hint:
Subqueries can appear almost anywhere in a query, including the select clause
SELECT longname, ROUND((bldgarea)/(SELECT SUM(bldgarea) FROM buildings) *100)
AS Percentage
From buildings
ORDER BY Percentage DESC
LIMIT 5;

2.6 Handling NULL values


1. Count the buildings whose long name is not known. Result: 15
SELECT COUNT(*)
FROM buildings
WHERE longname IS NULL;

2. Show the name and area of all buildings of facility type D, displaying “Unnamed
building” when the name is not known. Order by area descending. Result:
SELECT COALESCE(longname, 'unnamedBuilding'), SUM(bldgarea)
FROM buildings
WHERE (facilityke = 'D')
GROUP BY longname;

Вам также может понравиться