Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60aff14

Browse files
committed
add 07_tables.sql 07_where_clause.sql
1 parent e883474 commit 60aff14

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

07_tables.sql

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
-- Select Database
2+
drop database test_db;
3+
4+
create database test_db;
5+
6+
use test_db;
7+
8+
-- Employees Table
9+
drop table if exists employees;
10+
11+
CREATE TABLE employees (
12+
employee_id INT PRIMARY KEY,
13+
first_name VARCHAR(40) NOT NULL,
14+
last_name VARCHAR(40),
15+
email VARCHAR(100) NOT NULL,
16+
hire_date DATE NOT NULL,
17+
salary DECIMAL(10 , 2 ) NOT NULL DEFAULT 25000.00
18+
);
19+
20+
insert into employees(employee_id,first_name,last_name,email,hire_date,salary)
21+
values (1,'Michael',NULL,'[email protected]','2022-04-13',84630),
22+
(2,'Trevor','Philips','[email protected]','2022-04-21',25447),
23+
(3,'Franklin','Clinton','[email protected]','2022-04-21',30880),
24+
(4,'Wade','Hebert','[email protected]','2022-06-13',83835),
25+
(5,'Lester','Crest','[email protected]','2022-07-21',30586),
26+
(6,'Devin','Weston','[email protected]','2022-08-27',58948),
27+
(7,'Solomon','Richards','[email protected]','2022-08-27',57935),
28+
(8,'Jimmy',NULL,'[email protected]','2021-04-22',45375),
29+
(9,'Amanda',NULL,'[email protected]','2022-05-25',41732),
30+
(10,'Floyd',NULL,'[email protected]','2022-06-06',58178);
31+
32+
SELECT
33+
*
34+
FROM
35+
employees;
36+
37+
-- Customers Table
38+
drop table if exists customers;
39+
40+
CREATE TABLE customers (
41+
customer_id INT PRIMARY KEY,
42+
email VARCHAR(100) NOT NULL,
43+
first_name VARCHAR(20) NOT NULL,
44+
last_name VARCHAR(20),
45+
address VARCHAR(200) NOT NULL
46+
);
47+
48+
insert into customers(customer_id,first_name,last_name,email,address)
49+
values (1,'Michael',NULL,'[email protected]','Downtown'),
50+
(2,'Trevor','Philips','[email protected]','Rockford Hills'),
51+
(3,'Franklin','Clinton','[email protected]','Vinewood'),
52+
(4,'Jimmy',NULL,'[email protected]','Rockford Hills'),
53+
(5,'Amanda',NULL,'[email protected]','Rockford Hills'),
54+
(6,'Floyd',NULL,'[email protected]','Vinewood'),
55+
(7,'Wade','Hebert','[email protected]','Vinewood'),
56+
(8,'Lester','Crest','[email protected]','Downtown'),
57+
(9,'Devin','Weston','[email protected]','Downtown'),
58+
(10,'Solomon','Richards','[email protected]','Downtown');
59+
60+
SELECT
61+
*
62+
FROM
63+
customers;
64+
65+
-- Products Table
66+
drop table if exists products;
67+
68+
CREATE TABLE products (
69+
product_id INT PRIMARY KEY,
70+
product_name VARCHAR(100) NOT NULL,
71+
category VARCHAR(100) NOT NULL DEFAULT 'Others',
72+
price DECIMAL(10 , 2 ) NOT NULL CHECK (price > 0),
73+
product_desc VARCHAR(500) DEFAULT 'NA'
74+
);
75+
76+
insert into products(product_id,product_name,category,price)
77+
values (1,'iPhone 14','Mobile',71999),
78+
(2,'iPhone 14 Plus','Mobile',80999),
79+
(3,'Pixel 7','Mobile',57099),
80+
(4,'Pixel 7 Pro','Mobile',81199),
81+
(5,'Nothing Phone (1)','Mobile',35999),
82+
(6,'HP Pavilion','Gaming Laptop',62990),
83+
(7,'HP OMEN','Gaming Laptop',160990),
84+
(8,'Asus ROG','Gaming Laptop',90990),
85+
(9,'iPad','Tablet',29990),
86+
(10,'iPad Air','Tablet',53990);
87+
88+
-- Orders Table
89+
drop table if exists orders;
90+
91+
CREATE TABLE orders (
92+
order_id INT PRIMARY KEY,
93+
order_date DATE NOT NULL,
94+
fk_customer_id INT,
95+
total_amount DECIMAL(10 , 2 ) NOT NULL CHECK (total_amount > 0),
96+
status VARCHAR(20) DEFAULT 'Placed',
97+
FOREIGN KEY (fk_customer_id)
98+
REFERENCES customers (customer_id)
99+
);
100+
101+
insert into orders(order_id,order_date,fk_customer_id,total_amount,status)
102+
values(1,'2023-01-12',10,80999,'Delivered'),
103+
(2,'2023-01-20',8,57099,'Placed'),
104+
(3,'2023-01-21',8,81199,'Placed'),
105+
(4,'2023-01-24',7,35999,'Delivered'),
106+
(5,'2023-01-24',2,62990,'Delivered'),
107+
(6,'2023-01-25',10,160990,'Pending'),
108+
(7,'2023-02-02',4,62990,'Placed');
109+
110+
-- Order Items Table
111+
drop table if exists orders_items;
112+
113+
CREATE TABLE order_items (
114+
fk_order_id INT,
115+
fk_product_id INT,
116+
FOREIGN KEY (fk_order_id)
117+
REFERENCES orders (order_id),
118+
FOREIGN KEY (fk_product_id)
119+
REFERENCES products (product_id)
120+
);
121+
122+
insert into order_items(fk_order_id, fk_product_id)
123+
values (1,2),
124+
(2,3),
125+
(3,4),
126+
(4,5),
127+
(5,6),
128+
(5,3),
129+
(6,7),
130+
(7,6);

07_where_clause.sql

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use test_db;
2+
3+
-- 1. Write a query to retrieve all the data from the "employees" table where the employee_id is equal to 5.
4+
select *
5+
from employees
6+
where employee_id = 5;
7+
8+
-- 2. Write a query to retrieve the first and last names of all employees from the "employees" table who were hired in the year 2022.
9+
select *
10+
from employees
11+
where hire_date >= 2022-01-01;
12+
13+
-- 3. Write a query to retrieve the first and last names of all employees from the "employees" table who have a salary greater than 50,000.
14+
select first_name, last_name
15+
from employees
16+
where salary > 50000;
17+
18+
-- 4. Write a query to retrieve the product names and prices of all products from the "products" table that have a price greater than 45000.
19+
select product_name, price
20+
from products
21+
where price > 45000;
22+
23+
-- 5. Write a query to retrieve the order dates and total amounts of all orders from the "orders" table that have a total amount greater than 50000.
24+
select order_date, total_amount
25+
from orders
26+
where total_amount > 50000;
27+
28+
-- 6. Write a query to retrieve the first and last names of all customers from the "customers" table who live in Downtown.
29+
select first_name, last_name
30+
from customers
31+
where address = 'Downtown';
32+
33+
-- 7. Write a query to retrieve the order dates and total amounts of all orders from the "orders" table that were placed in the month of January.
34+
select order_date, total_amount
35+
from orders
36+
where MONTH(order_date) = 1;
37+
38+
-- 8. Write a query to retrieve the order dates and total amounts of all order from the "order" table that have a status of "Pending".
39+
select order_date, total_amount, status
40+
from orders
41+
where status = 'Pending';
42+
43+
-- 9. Write a query to retrieve the product names and prices of all products from the "products" table that belong to the "Tablet" category and have a price less than 28000.
44+
select product_name, price
45+
from products
46+
where category = 'Tablet'
47+
and price < 28000;
48+
49+

0 commit comments

Comments
 (0)