-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL _Task_KR.sql
More file actions
35 lines (28 loc) · 772 Bytes
/
SQL _Task_KR.sql
File metadata and controls
35 lines (28 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
amount DECIMAL(10, 2)
);
INSERT INTO orders (order_id, customer_id, order_date, amount) VALUES
(1, 101, '2023-01-01', 250.00),
(2, 102, '2023-01-02', 150.00),
(3, 101, '2023-01-05', 100.00),
(4, 103, '2023-01-07', 300.00);
-- Total amount spent by each customer.
SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id;
-- Orders placed after '2023-01-03'.
SELECT *
FROM orders
WHERE order_date > '2023-01-03';
-- Customers who made more than one order.
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 1;
-- If you want to see all the data at any time.
SELECT * FROM orders;