-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTables.sql
51 lines (44 loc) · 1.31 KB
/
MyTables.sql
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- Create a new database
CREATE DATABASE IF NOT EXISTS SampleDB;
USE SampleDB;
-- Create a table for storing information about users
CREATE TABLE IF NOT EXISTS Users (
UserID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100) UNIQUE
);
-- Create a table for storing information about orders
CREATE TABLE IF NOT EXISTS Orders (
OrderID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
OrderDate DATE,
TotalAmount DECIMAL(10, 2),
FOREIGN KEY (UserID) REFERENCES Users(UserID)
);
-- Insert sample data into Users table
INSERT INTO Users (FirstName, LastName, Email) VALUES
('John', 'Doe', '[email protected]'),
('Jane', 'Smith', '[email protected]'),
('Michael', 'Johnson', '[email protected]');
-- Insert sample data into Orders table
INSERT INTO Orders (UserID, OrderDate, TotalAmount) VALUES
(1, '2023-08-25', 150.00),
(2, '2023-08-26', 200.00),
(3, '2023-08-26', 75.50);
-- Query to retrieve user's first name, last name, and total order amount
SELECT
u.FirstName,
u.LastName,
SUM(o.TotalAmount) AS TotalSpent
FROM
Users u
JOIN
Orders o ON u.UserID = o.UserID
GROUP BY
u.UserID;
-- Drop the tables (optional)
-- DROP TABLE Orders;
-- DROP TABLE Users;
-- Drop the database (optional)
-- DROP DATABASE SampleDB;