1907. Count Salary Categories

SQL 50道

内容持续更新…(始于2024年08月03日)

1.问题

Table: Accounts

±------------±-----+
| Column Name | Type |
±------------±-----+
| account_id | int |
| income | int |
±------------±-----+
account_id is the primary key (column with unique values) for this table.
Each row contains information about the monthly income for one bank account.

Write a solution to calculate the number of bank accounts for each salary category. The salary categories are:

“Low Salary”: All the salaries strictly less than $20000.
“Average Salary”: All the salaries in the inclusive range [$20000, $50000].
“High Salary”: All the salaries strictly greater than $50000.
The result table must contain all three categories. If there are no accounts in a category, return 0.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:
Accounts table:
±-----------±-------+
| account_id | income |
±-----------±-------+
| 3 | 108939 |
| 2 | 12747 |
| 8 | 87709 |
| 6 | 91796 |
±-----------±-------+
Output:
±---------------±---------------+
| category | accounts_count |
±---------------±---------------+
| Low Salary | 1 |
| Average Salary | 0 |
| High Salary | 3 |
±---------------±---------------+
Explanation:
Low Salary: Account 2.
Average Salary: No accounts.
High Salary: Accounts 3, 6, and 8.

2. 解题思路

方法1:

  1. SUM(CASE WHEN … THEN 1 ELSE 0 END):
  • 这里使用 CASE 语句根据不同的收入范围分类。
  • SUM 函数对 CASE 语句的结果进行求和,计算每个分类中的账户数量。CASE 语句会返回 1 或 0,根据条件是否满足。
  1. UNION ALL:
  • 将三个查询结果合并成一个结果集。UNION ALL 用于包含所有记录,不去除重复行(这里没有重复行问题)。
  1. 每个查询:
  • ‘Low Salary’: 计算收入少于 20000 的账户数量。
  • ‘Average Salary’: 计算收入在 20000 到 50000 之间(包含)的账户数量。
  • ‘High Salary’: 计算收入高于 50000 的账户数量。

3. 代码

代码1:

SELECT 
    'Low Salary' AS category,
    SUM(CASE WHEN income < 20000 THEN 1 ELSE 0 END) AS accounts_count
FROM Accounts

UNION ALL

SELECT 
    'Average Salary' AS category,
    SUM(CASE WHEN income BETWEEN 20000 AND 50000 THEN 1 ELSE 0 END) AS accounts_count
FROM Accounts

UNION ALL

SELECT 
    'High Salary' AS category,
    SUM(CASE WHEN income > 50000 THEN 1 ELSE 0 END) AS accounts_count
FROM Accounts;

select 'High Salary' as category, (select count(*) from Accounts where income > 50000) as accounts_count
union all
select 'Average Salary' as category, (select count(*) from Accounts where income >= 20000 and income <= 50000) as accounts_count
union all
select 'Low Salary' as category, (select count(*) from Accounts where income < 20000) as accounts_count
好的,以下是 SQL Server 语句实现: 1. 创建数据库: ``` CREATE DATABASE mydatabase; USE mydatabase; ``` 2. 创建表并插入数据: ``` CREATE TABLE customers ( id INT PRIMARY KEY, name VARCHAR(50), age INT, city VARCHAR(50) ); INSERT INTO customers (id, name, age, city) VALUES (1, 'John', 25, 'New York'), (2, 'Mary', 30, 'Los Angeles'), (3, 'David', 40, 'Chicago'), (4, 'Sarah', 35, 'Houston'), (5, 'Michael', 28, 'Miami'), (6, 'Jennifer', 32, 'San Francisco'), (7, 'Robert', 45, 'Atlanta'), (8, 'Lisa', 27, 'Seattle'), (9, 'Kevin', 38, 'Dallas'), (10, 'Jessica', 33, 'Boston'); CREATE TABLE orders ( id INT PRIMARY KEY, customer_id INT, product VARCHAR(50), price DECIMAL(10, 2), order_date DATE, FOREIGN KEY (customer_id) REFERENCES customers(id) ); INSERT INTO orders (id, customer_id, product, price, order_date) VALUES (1, 1, 'Phone', 500, '2021-05-01'), (2, 1, 'Laptop', 1000, '2021-06-01'), (3, 2, 'Tablet', 700, '2021-05-15'), (4, 3, 'TV', 1200, '2021-05-20'), (5, 4, 'Headphones', 200, '2021-06-05'), (6, 5, 'Camera', 800, '2021-06-10'), (7, 6, 'Smartwatch', 300, '2021-06-15'), (8, 7, 'Speaker', 150, '2021-07-01'), (9, 8, 'Printer', 400, '2021-07-05'), (10, 9, 'Monitor', 600, '2021-07-10'); CREATE TABLE products ( id INT PRIMARY KEY, name VARCHAR(50), category VARCHAR(50), price DECIMAL(10, 2) ); INSERT INTO products (id, name, category, price) VALUES (1, 'Phone', 'Electronics', 500), (2, 'Laptop', 'Electronics', 1000), (3, 'Tablet', 'Electronics', 700), (4, 'TV', 'Electronics', 1200), (5, 'Headphones', 'Accessories', 200), (6, 'Camera', 'Electronics', 800), (7, 'Smartwatch', 'Accessories', 300), (8, 'Speaker', 'Electronics', 150), (9, 'Printer', 'Electronics', 400), (10, 'Monitor', 'Electronics', 600); CREATE TABLE categories ( id INT PRIMARY KEY, name VARCHAR(50) ); INSERT INTO categories (id, name) VALUES (1, 'Electronics'), (2, 'Accessories'); CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50), salary DECIMAL(10, 2) ); INSERT INTO employees (id, name, department, salary) VALUES (1, 'John', 'Sales', 5000), (2, 'Mary', 'Marketing', 6000), (3, 'David', 'IT', 7000), (4, 'Sarah', 'HR', 4000), (5, 'Michael', 'Finance', 8000), (6, 'Jennifer', 'Operations', 5500), (7, 'Robert', 'Engineering', 9000), (8, 'Lisa', 'Legal', 4500), (9, 'Kevin', 'Customer Service', 3500), (10, 'Jessica', 'Management', 10000); ``` 3. 使用模式匹配查询: ``` SELECT name, age, city FROM customers WHERE name LIKE 'J%'; ``` 4. 使用 TOP 查询: ``` SELECT TOP 5 name, age, city FROM customers ORDER BY age DESC; ``` 5. 使用 IN 查询: ``` SELECT name, product, price FROM orders WHERE customer_id IN (1, 3, 5); ``` 6. 使用降序查询: ``` SELECT name, salary FROM employees ORDER BY salary DESC; ``` 7. 使用 COUNT 集合函数查询: ``` SELECT COUNT(*) AS num_customers FROM customers; ``` 8. 分组统计查询: ``` SELECT category, AVG(price) AS avg_price, COUNT(*) AS num_products FROM products GROUP BY category; ``` 9. 使用连接条件的多表查询: ``` SELECT customers.name, orders.product, orders.price FROM customers INNER JOIN orders ON customers.id = orders.customer_id; ``` 10. 比较运算符的子查询: ``` SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees); ``` 11. 使用 IN 的子查询: ``` SELECT name, department FROM employees WHERE department IN (SELECT name FROM categories WHERE name LIKE 'M%'); ``` 12. 创建视图并使用: ``` CREATE VIEW customer_orders AS SELECT customers.name, orders.product, orders.price FROM customers INNER JOIN orders ON customers.id = orders.customer_id; SELECT * FROM customer_orders; ``` 13. 创建索引: ``` CREATE INDEX idx_customer_age ON customers(age); SELECT name, age FROM customers WHERE age BETWEEN 30 AND 40; ``` 14. 创建约束: ``` ALTER TABLE customers ADD CONSTRAINT uc_customer_name UNIQUE (name); ALTER TABLE orders ADD CONSTRAINT fk_orders_customer_id FOREIGN KEY (customer_id) REFERENCES customers(id); ALTER TABLE employees ADD CONSTRAINT chk_employee_salary CHECK (salary >= 3000 AND salary <= 10000); ALTER TABLE products ADD CONSTRAINT df_product_price DEFAULT 0; ``` 15. 创建存储过程并使用: ``` CREATE PROCEDURE get_customer_orders @customer_id INT AS BEGIN SELECT product, price, order_date FROM orders WHERE customer_id = @customer_id; END; EXEC get_customer_orders 1; ``` 16. 创建触发器: ``` CREATE TRIGGER tr_customer_orders ON orders AFTER INSERT, UPDATE AS BEGIN UPDATE customers SET last_order_date = GETDATE() WHERE customers.id = inserted.customer_id; END; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值