Dbms
Dbms
Dbms
1
2. Create Table students (s_id, s_name, s_roll, s_age, s_phone) and
insert 5 records. Add primary key constraints to s_id and unique
constraints to name. Display records and demonstrates.
2
3. Create above student table using default constraints in any column
and truncate the result and again insert the records and see the
result. Code to create table with default constraints:
3
4. Create table customer (c_id, c_name, c_address, c_phone) and orders
(o_id, o_name, o_qty, o_amt, c_id) and insert 10 records in customer and 5
records in orders tables respectively. Write query to retrieve records.
i. SQL to retrieve customers details.
ii. SQL to retrieve customer details whose order amount is maximum.
iii. SQL to find customers name whose address is Kathmandu.
iv. SQL to count orders of each customer.
v. SQL to delete records whose order amount > 4000.
vi. give 10% discount to those customers whose order amount is less than 3000
vii. truncate the records from customers tables.
4
ii. Code to retrieve customers detail whose order amount is maximum.
SELECT c.*
FROM customer c
INNER JOIN (
SELECT c_id, MAX(o_amt) AS max_amt
FROM orders
GROUP BY c_id
) o ON c.c_id = o.c_id;
5
FROM customer c
LEFT JOIN orders o ON c.c_id = o.c_id
GROUP BY c.c_id, c.c_name;
vi. To give 10% discount to those customers whose order amount is less than 3000:
UPDATE orders
SET o_amt = o_amt * 0.9
WHERE o_amt < 3000.00;
6
5. Create table Customers and insert as following:
7
ii. SQL to delete records whose address is Kathmandu or Teku.
DELETE FROM customers
WHERE address IN ('Kathamandu', 'Teku');
iii. SQL to update address to Kalanki whose age is greater than 25.
UPDATE customers
SET address = 'Kalanki'
WHERE age > 25;
iv. SQL to display records whose age is greater than 22 and address is Kathmadnu.
SELECT * FROM customers
WHERE age > 22 AND address = 'Kathamandu';
v. SQL to display name, age and address whose salary is less than 5000.
SELECT name, age, address FROM customers
WHERE salary < 5000;
8
6. Books Tables (bookid, titlem authorfirst,
authorlast,category,qty,price):
Create this table and insert 5 records.
• SQL to display top 3 records.
• SQL to display the records whose price is between 200 and 300.
• SQL to display the records of those books whose qty is greater than
those books whose price is greater than 500.
• SQL to update qty whose prices is greater than 500.
• SQL to delete those records qty is greater than 5 or prices is less than
3000.
CREATE TABLE Books (
bookid INT PRIMARY KEY,
title VARCHAR(100),
authorfirst VARCHAR(50),
authorlast VARCHAR(50),
category VARCHAR(50),
qty INT,
price DECIMAL(10, 2)
);
INSERT INTO Books (bookid, title, authorfirst, authorlast, category, qty, price)
VALUES
(1, 'Overcoming gravity', 'Steven', 'Low', 'Fitness', 1, 900.00),
(2, 'Dracula', 'Bram', 'Stoker', 'Fiction', 5, 200.00),
(3, 'Ghost in the wires ', 'Kevin', 'Mitnick', 'Biography', 15, 130.00),
(4, 'Social Engineering', 'Christopher', 'Hadnagy', 'Science', 20, 250.00),
(5, 'Outwitting The Devil', 'Napoleon', 'Hill', 'Self-help', 8, 600.00);
SQL to display the records whose price is between 200 and 300:
SELECT * FROM Books
WHERE price BETWEEN 200 AND 300;
9
SQL to display the records of those books whose qty is greater than those books whose
price is greater than 500:
SELECT * FROM Books
WHERE qty > (
SELECT qty FROM Books
WHERE price > 500
);
SQL to delete those records whose qty is greater than 5 or prices is less than 3000:
DELETE FROM Books
WHERE qty > 5 OR price < 3000.00;
10
7. Students (s_id, s_name, s_age, s_address, s_marks) Create this table and
insert 10 records:
• SQL to display records in ascending order using name attribute.
• SQL to find of mark according to name.
• SQL to display distinct mark in descending order
• SQL to count id according to address where count is greater than 3
11
Code to find of mark according to name:
SELECT s_marks
FROM Students
WHERE s_name = 'Sita Magar’;
12
8. Inner join Operation in SQL
Table CUSTOMERS:
13
INSERT INTO ORDERS (OID, DATE, CUSTOMER_ID, AMOUNT)
VALUES
(102, '2009-10-08 00:00:00', 3, 3000),
(100, '2009-10-08 00:00:00', 3, 1500),
(101, '2009-11-20 00:00:00', 2, 1560),
(103, '2008-05-20 00:00:00', 4, 2060);
14
9. The following tables write a SQL query to find the salesperson and
customer who belongs to same city. Return Salesman, cust_name and city.
15
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id)
VALUES
(3002, 'Nick Rimando', 'New York', 100, 5001),
(3007, 'Brad Davis', 'New York', 200, 5001),
(3005, 'Graham Zusi', 'California', 200, 5002),
(3008, 'Julian Green', 'London', 300, 5002),
(3004, 'Fabian Johnson', 'Paris', 300, 5006),
(3009, 'Geoff Cameron', 'Berlin', 100, 5003),
(3003, 'Jozy Altidor', 'Moscow', 200, 5007),
(3001, 'Brad Guzan', 'London', NULL, 5005);
SQL Query to find the salesperson and customer who belongs to same city:
SELECT salesman.name AS "Salesman",
customer.cust_name, customer.city
FROM salesman,customer
WHERE salesman.city=customer.city;
16
10. From the following tables write a SQL query to find those orders where
the order amount exists between 500 and 2000. Return ord_no, purch_amt,
cust_name, city.
17
INSERT INTO customers (customer_id, cust_name, City, Grade, salesman_id)
VALUES
(3002, 'Nick Rimando', 'New York', 100, 5001),
(3007, 'Brad Davis', 'New York', 200, 5001),
(3005, 'Graham Zusi', 'California', 200, 5002),
(3008, 'Julian Green', 'London', 300, 5002),
(3004, 'Fabian Johnson', 'Paris', 300, 5006),
(3009, 'Geoff Cameron', 'Berlin', 100, 5003),
(3003, 'Jozy Altidor', 'Moscow', 200, 5007),
(3001, 'Brad Guzan', 'London', NULL, 5005);
SQL query to find those orders where order amount exists between 500 and 2000.
Return ord_no, purch_amt, cust_name, city:
SELECT a.ord_no,a.purch_amt,
b.cust_name,b.city
FROM orders a,customers b
WHERE a.customer_id=b.customer_id
AND a.purch_amt BETWEEN 500 AND 2000;
18
11. From the following tables write a SQL query to find the salesperson(s)
and the customer(s) he handles. Return Customer Name, city, Salesman,
commission.
19
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 3)
);
Output:
20
12. From the following tables write a SQL query to find those salespersons
who received a commission from the company more than 12%. Return
Customer Name, customer city, Salesman, commission.
21
SQL query to find those salespersons who received a commission from the company
more than 12%. Return Customer Name, customer city, Salesman, commission:
22
13. From the following tables write a SQL query to find those salespersons
do not live in the same city where their customers live and received a
commission from the company more than 12%. Return Customer Name,
customer city, Salesman,salesman city, commission.
SQL query to find those salespersons do not live in the same city where their customers
live and received a commission from the company more than 12%. Return Customer
Name, customer city, Salesman,salesman city, commission.
23
14. Write a SQL statement to make a join on the tables salesman, customer
and orders in such a form that the same column of each table will appear
once and only the relational rows will come.
24
SQL query to make a join on the tables salesman, customer and orders in such a form
that the same column of each table will appear once and only the relational rows will
come:
SELECT *
FROM orders
NATURAL JOIN customer
NATURAL JOIN salesman;
25
15. From the following tables write a SQL query to find those customers
whose grade less than 300. Return cust_name,customer city, grade,
Salesman, saleman city. The result should be ordered by ascending
customer_id.
SQL query to find those customers whose grade less than 300. Return cust_name,
customer city, grade, Salesman, saleman city. The result should be ordered by ascending
customer_id:
SELECT
c.cust_name AS "Customer Name",
c.city AS "Customer City",
c.grade,
s.name AS "Salesman",
s.city AS "Salesman City"
FROM customer c
JOIN salesman s ON c.salesman_id = s.salesman_id
WHERE c.grade < 300
ORDER BY c.customer_id ASC;
26
27