DB Lab
DB Lab
Output:
Lab 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.
Code to create table:
CREATE TABLE students (
s_id INT PRIMARY KEY,
s_name VARCHAR(50) UNIQUE,
s_roll INT,
s_phone VARCHAR(15)
);
Code to insert:
INSERT INTO students (s_id, s_name, s_roll, s_age, s_phone) VALUES
(1, 'Samir Paudel', 4, 20, 9840899454),
(2,'Prabhat Gurung',5,22,9803498579),
(3,'Aaryak Pradhan',6,21,9848749875),
(4,'Dipesh Dawadi',9,20,98237598498),
(9,'Manoj Magar',1,21,9844549875);
Code to show:
SELECT * FROM students;
Output:
Lab 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.
Code to Create tables and insert into it:
CREATE TABLE customer (
c_id INT PRIMARY KEY,
c_name VARCHAR(50),
c_address VARCHAR(100),
c_phone VARCHAR(15)
);
INSERT INTO customer (c_id, c_name, c_address, c_phone)
VALUES
(1, 'Ram Bahadur', 'kailali', '9802384756'),
(2, 'Hari Kumar', 'Bhaktapur', '9802384334'),
(3, 'Krishna BK', 'Sindhuli', '988394859'),
(4, 'Prabhat Gurung', 'Pokhara', '9898456125'),
(5, 'Dipesh Dawadi', 'Kathmandu', '9878954621'),
(6, 'Aaryak Pradhan', 'Kathmandu', '984587958'),
(7, ‘Dip Khadka', 'Kathmandu', '9898745612'),
(8, 'Manoj Magar', 'Bhaktapur', '9878549135),
(9, 'Hari Bahadur', 'Lalitpur', '9812546387'),
(10, 'Tiji Jojo', 'Kawasaki', '0801234578');
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;
iii.Sql to display name, age and address whose salary is less than 5000.
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
address VARCHAR(100),
salary DECIMAL(10, 2)
);
INSERT INTO customers (id, name, age, address, salary)
VALUES
(1, 'Ramesh', 32, 'Kathmandu', 2000.00),
(2, 'Khilan', 25, 'Kupondole', 1500.00),
(3, 'Kaushik', 23, 'Kalimati', 2000.00),
(4, 'Chaitali', 25, 'Kalanki', 6500.00),
(5, 'Hardik', 27, 'Tripureshwor', 8500.00),
(6, 'Komal', 22, 'Teku', 4500.00),
(7, 'Muffy', 24, 'Gaushala', 10000.00);
SELECT name, age, address FROM customers WHERE salary < 5000;
Output:
INSERT INTO Books (bookid, title, authorfirst, authorlast, category, qty, price)
VALUES
(1, 'Book 1', 'Author 1', 'Lastname 1', 'Fiction', 10, 150.99),
(2, 'Book 2', 'Author 2', 'Lastname 2', 'Mystery', 8, 250.50),
(3, 'Book 3', 'Author 3', 'Lastname 3', 'Fantasy', 5, 180.99),
(4, 'Book 4', 'Author 4', 'Lastname 4', 'Science Fiction', 12, 560.25),
(5, 'Book 5', 'Author 5', 'Lastname 5', 'Non-Fiction', 15, 210.75);
SELECT * FROM Books LIMIT 3;
Output
ii. Sql to display the records whose price is between 200 and 300.
SELECT * FROM Books WHERE price BETWEEN 200 AND 300;
Output
iii. 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 MAX(qty) FROM Books WHERE price
> 500);
Output
Lab 7: Students (s_id, s_name, s_age, s_address, s_marks)
Create this table and insert 5 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
Code to create table and insert:
CREATE TABLE Students (
s_id INT PRIMARY KEY,
s_name VARCHAR(100),
s_age INT,
s_address VARCHAR(100),
s_marks DECIMAL(5, 2)
);
SQL code for creating the table and inserting in that table:
CREATE TABLE CUSTOMERS (
ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
SALARY DECIMAL(10, 2)
);
SQL Code for creating the table and inserting into table orders:
CREATE TABLE ORDERS (
OID INT PRIMARY KEY,
DATE TIMESTAMP,
CUSTOMER_ID INT,
AMOUNT DECIMAL(10, 2),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID)
);
Output:
Lab 9: From the following tables write a SQL query to find the salesperson and
customer who belongs to same city. Return Salesman, cust_name and city.
Sample table: Salesman
salesman_id | name | city | commission
---------------+--------------------+---------------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome |0.13
5003 | Lauson Hen | San Jose |0.12
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;
Output:
Lab 10:From the following tables write a SQL query to find those orders where
order amount exists between 500 and 2000.
Return ord_no, purch_amt, cust_name, city.
Sample table: orders
ord_no purch_amt ord_date customer_id Salesman_id
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
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;
Output:
Lab 11: From the following tables write a SQL query to find the salesperson(s)
and the customer(s) he handle. Return Customer
Name, city, Salesman, commission.
Sample table: customer
customer_id |cust_name|city| grade | salesman_id
-------------+----------------+------------+-------+-------------
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||5005
Sample table: salesman
salesman_id |name|city| commission
-------------+------------+----------+------------
5001 | James Hoog | New York |0.15
5002 | Nail Knite | Pari|0.135005
5005|Pit Alex | London| 0.11
5006| Mc Lyon| Paris | 0.14
5007| Paul Adam | Rome | 0.13
5003 | Lauson Hen| San Jose| 0.12
SQL query to find the salesperson(s) and the customer(s) he handle. Return Customer
Name, city, Salesman, commission
SELECT a.cust_name AS "Customer Name",
a.city, b.name AS "Salesman", b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id;
Output:
Lab 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.
Sample table: customer
customer_id |cust_name|city| grade | salesman_id
-------------+----------------+------------+-------+-------------
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||5005
SQL query to find those salespersons who received a commission from the company
more than 12%. Return Customer Name, customer city, Salesman, commission:
SELECT a.cust_name AS "Customer Name",
a.city, b.name AS "Salesman", b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id
WHERE b.commission>.12;
Output:
Lab 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.
customer_id |cust_name|city| grade | salesman_id
-------------+----------------+------------+-------+-------------
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||5005
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.
Output:
Lab 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.
Sample table: orders
ord_no purch_amt ord_date customer_id salesman_id
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
Output:
Lab 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.
Sample table: customer
customer_id |cust_name|city| grade | salesman_id
-------------+----------------+------------+-------+-------------
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|100 |5005
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;
Output: