Lab1答案与解析

在这里插入图片描述

-- Lab1
-- 1. List all staff details.
SELECT 
    *
FROM
    staff;

-- 2. List staff whose salary is between 8,000 and 20,000. Show staff name, position and salary.
SELECT 
    fName, lName, position, salary
FROM
    staff
WHERE
    salary BETWEEN 8000 AND 20000;

-- 3. List details of staff in branch B003 and B007.
SELECT 
    *
FROM
    staff
WHERE
    branchNo = 'B003' OR branchNo = 'B007';

-- 4. Display all the distinct different types of positions of staff.
SELECT DISTINCT
    position
FROM
    staff;

-- 5. List all branch offices in London, give branch number and address.
SELECT 
    branchNo, street
FROM
    Branch
WHERE
    city = 'london';

-- 6. List the employees whose first names have 'an' in them.
SELECT 
    staffNo, fName, lName
FROM
    Staff
WHERE
    fName LIKE '%an%';

-- 7. List the client numbers and names, order by client number.
SELECT 
    clientNo, fName, IName
FROM
    Client
ORDER BY clientNo;

-- 8. Find the property number and address of all properties for rent which has not been assigned to a staff.
SELECT 
    propertyNo, street
FROM
    PropertyForRent
WHERE
    staffNo IS NULL;

-- 9. How many properties for rent are managed in each branch?
SELECT 
    branchNo, COUNT(propertyNo)
FROM
    PropertyForRent
GROUP BY branchNo;

-- 10.  List all properties for rent in descending order of their rent.
SELECT 
    *
FROM
    PropertyForRent
ORDER BY rent DESC;

-- 11. Calculate the annual rent for all properties for rent (the rent in DreamHome Database is monthly rent).
SELECT 
    propertyNo, rent * 12 AS annualRent
FROM
    PropertyForRent;

-- 12. Find all the staff who is not a manager and with a salary greater than or equal to 12,000.
SELECT 
    staffNo, fName, lName, position, salary
FROM
    Staff
WHERE
    (position != 'Manager')
        AND salary >= 12000;

-- 13. Find out how many managers there are without listing them.
SELECT 
    COUNT(staffNo) AS managerNumber
FROM
    Staff
WHERE
    position = 'Manager';

-- 14. Calculate average salary for all staff.
SELECT 
    AVG(salary) AS avgSalary
FROM
    Staff;

-- 15. Calculate average salary for staff at each Branch.
SELECT 
    branchNo, AVG(salary)
FROM
    Staff
GROUP BY branchNo;

-- 16.For each branch with more than 1 member of staff, find number of staff in each branch and their average salaries.
SELECT 
    branchNo, COUNT(staffNo), AVG(salary) AS avgSalary
FROM
    Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1;



-- 17.Find the highest salary and lowest salary of staff and the difference between them.
SELECT 
    MAX(salary),
    MIN(salary),
    MAX(salary) - MIN(salary) AS difference
FROM
    Staff;

-- 18. Count the number of properties for rent with 3 rooms.
SELECT 
    COUNT(propertyNo)
FROM
    PropertyForRent
WHERE
    rooms = 3;

-- 19. Find the average salary for assistants and managers.
SELECT 
    AVG(salary) AS avgSalary
FROM
    Staff
WHERE
    position = 'Manager'
        OR position = 'Assistant';

-- 20.Calculate the weekly rent for all properties for rent, and round the results to the nearest penny. Assuming there are 4 weeks in a month. (Use ROUND(X,D) function, this rounds the argument X to D decimal places.)
SELECT 
    ROUND(AVG(rent) / 4, 2) AS weeklyRent
FROM
    PropertyForRent;

-- 21. List the properties for rent which have not been viewed at all. (Hint: use subquery)
SELECT 
    propertyNo
FROM
    PropertyForRent
WHERE
    propertyNo NOT IN (SELECT 
            propertyNo
        FROM
            Viewing);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值