0% found this document useful (0 votes)
32 views

DB Lab

Lab report bca

Uploaded by

rishavpuri339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

DB Lab

Lab report bca

Uploaded by

rishavpuri339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Lab1:Create database employeedb.

Create table employee with attributes emp_id,emp_name,emp_address,emp_salary,emp_phone.


Insert 10 records. And Display result.
Code to create the database and use it:
CREATE DATABASE employeedb;
USE employeedb;

Code to Create table:


CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_address VARCHAR(100),
emp_salary DECIMAL(10, 2),
emp_phone VARCHAR(15)
);

Code to insert into table employee:


INSERT INTO employee (emp_id, emp_name, emp_address, emp_salary, emp_phone)
VALUES
(1, 'Samir Paudel', 'Fukoka', 500000, 9840899454),
(2,'Prabhat Gurung','Naikap',25000,9803498579),
(3,'Aaryak Pradhan','Naikap',30000,9848749875),
(4,'Dipesh Dawadi','Naikap',1000000,98237598498),
(5,'Mahesh Dalle','Bihar',5000,9858734857),
(7,'Pablo Escabar','Italy',300000000,17973947985),
(8,'Dip khadka','kalanki',0,9192631770),
(9,'Manoj Magar','Satungal',35000,9844549875),
(10,'Krishna BK','Naikap',30000,9848769475);

Code to display result:


SELECT * FORM employee;

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');

CREATE TABLE orders (


o_id INT PRIMARY KEY,
o_name VARCHAR(50),
o_qty INT,
o_amt DECIMAL(10, 2),
c_id INT,
FOREIGN KEY (c_id) REFERENCES customer(c_id)
);

INSERT INTO orders (o_id, o_name, o_qty, o_amt, c_id)


VALUES
(1, 'Logitech G-29', 5, 80000.00, 7),
(2, 'PS5', 2, 75000.00, 10),
(3, 'Xiomi Note 9', 1, 29000.00, 4),
(4, 'USB C', 2, 250.00, 1),
(5, 'Wireless Adapter', 5, 2000.00, 2);

Code to retrieve Customers details:


SELECT * FROM customer;
Output:

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;
Output:
To find customers name whose address is Kathmandu:
SELECT c_name
FROM customer
WHERE c_address = 'Kathmandu';
Output:

To count orders of each customer:


SELECT c.c_name, COUNT(o.o_id) AS order_count
FROM customer c
LEFT JOIN orders o ON c.c_id = o.c_id
GROUP BY c.c_id, c.c_name;
Output:

To delete records whose order amount > 4000:


DELETE FROM orders
WHERE o_amt > 4000.00;

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;

To truncate the records from the customer table:


TRUNCATE TABLE customer;
Lab 5: Create table Customers and insert as following:
Id Name Age Address Salary
1 Ramesh 32 Kathmandu 2000
2 Khilan 25 Kupondole 1500
3 Kaushik 23 Kalimati 2000
4 Chaitali 25 Kalanki 6500
5 Hardik 27 Tripureshowr 8500
6 Komal 22 Teku 4500
7 Muffy 24 Caushala 10000
Code to create the table above and insert the above data:
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),
(2, 'Khilan', 25, 'Kupondole', 1500),
(3, 'Kaushik', 23, 'Kalimati', 2000),
(4, 'Chaitali', 25, 'Kalanki', 6500),
(5, 'Hardik', 27, 'Tripureshwor', 8500),
(6, 'Komal', 22, 'Teku', 4500),
(7, 'Muffy', 24, 'Gaushala', 10000);

i. SQL to display records whose salary is greater than 5000.


SELECT * FROM customers
WHERE salary > 5000;
Output
ii.Sql to display records whose age is greater than 22 and address is Kathmandu.
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 * FROM customers WHERE age > 22 AND address = 'Kathmandu';
Output

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:

Lab 6: Books Tables (bookid, titlem authorfirst, authorlast,category,qty,price):


Create this table and insert 5 records.
 SQL to display top 3 records.
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, '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)
);

INSERT INTO Students (s_id, s_name, s_age, s_address, s_marks)


VALUES
(1, 'Alice', 20, '123 Main St', 85.50),
(2, 'Bob', 22, '456 Elm St', 78.75),
(3, 'Charlie', 21, '789 Oak St', 92.25),
(4, 'David', 23, '101 Pine St', 65.00),
(5, 'Eve', 24, '202 Maple St', 89.75);
SELECT * FROM Students ORDER BY s_name ASC;
Output

 SQL to find of mark according to name.


Code: SELECT s_name, SUM(s_marks) AS total_marks FROM Students GROUP BY s_name;
Output
Iii SQL to display distinct mark in descending order
Code: SELECT DISTINCT s_marks FROM Students ORDER BY s_marks DESC;
Output
Lab 8: Inner join Operation in SQL
Table CUSTOMERS:
ID Name Age ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 Mp 4500.00
7 Muffy 24 Indore 10000.00

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)
);

INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)


VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00),
(2, 'Khilan', 25, 'Delhi', 1500.00),
(3, 'Kaushik', 23, 'Kota', 2000.00),
(4, 'Chaitali', 25, 'Mumbai', 6500.00),
(5, 'Hardik', 27, 'Bhopal', 8500.00),
(6, 'Komal', 22, 'MP', 4500.00),
(7, 'Muffy', 24, 'Indore', 10000.00);

Table 2- ORDERS Table


OID DATE CUSTOMER_ID AMOUNT
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

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)
);

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);

SQL code to create inner join:


SELECT ID, NAME, AGE, AMOUNT
FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_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

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 create table Salesman and insert into it:


CREATE TABLE Salesman (
salesman_id INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 2)
);

INSERT INTO Salesman (salesman_id, name, city, commission)


VALUES
(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 for table Customer:


CREATE TABLE customer (
customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT
);

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;

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

Sample table: customers


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 create table and insert into it:


CREATE TABLE orders (
ord_no INT PRIMARY KEY,
purch_amt DECIMAL(10, 2),
ord_date DATE,
customer_id INT,
salesman_id INT
);

INSERT INTO orders (ord_no, purch_amt, ord_date, customer_id, salesman_id)


VALUES
(70001, 150.50, '2012-10-05', 3005, 5002),
(70009, 270.65, '2012-09-10', 3001, 5005),
(70002, 65.26, '2012-10-05', 3002, 5001),
(70004, 110.50, '2012-08-17', 3009, 5003),
(70007, 948.50, '2012-09-10', 3005, 5002),
(70005, 2400.60, '2012-07-27', 3007, 5001),
(70008, 5760.00, '2012-09-10', 3002, 5001),
(70010, 1983.43, '2012-10-10', 3004, 5006),
(70003, 2480.40, '2012-10-10', 3009, 5003),
(70012, 250.45, '2012-06-27', 3008, 5002),
(70011, 75.29, '2012-08-17', 3003, 5007),
(70013, 3045.60, '2012-04-25', 3002, 5001);

CREATE TABLE customers (


customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
City VARCHAR(50),
Grade INT,
salesman_id INT
);

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;

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

Code to create table and insert into it:

CREATE TABLE customer (


customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT
);

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);

CREATE TABLE salesman (


salesman_id INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 3)
);

INSERT INTO salesman (salesman_id, name, city, commission)


VALUES
(5001, 'James Hoog', 'New York', 0.150),
(5002, 'Nail Knite', 'Paris', 0.135005),
(5005, 'Pit Alex', 'London', 0.110),
(5006, 'Mc Lyon', 'Paris', 0.140),
(5007, 'Paul Adam', 'Rome', 0.130),
(5003, 'Lauson Hen', 'San Jose', 0.120);

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

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

Code to Create the tables and insert into it:

CREATE TABLE customer (


customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT
);

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);

CREATE TABLE salesman (


salesman_id INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 2)
);
INSERT INTO salesman (salesman_id, name, city, commission)
VALUES
(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 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

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 create and insert into table:


CREATE TABLE customer (
customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT
);

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);

CREATE TABLE salesman (


salesman_id INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 2)
);
INSERT INTO salesman (salesman_id, name, city, commission)
VALUES
(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 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.

SELECT a.cust_name AS "Customer Name",


a.city, b.name AS "Salesman", b.city,b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id
WHERE b.commission>.12
AND a.city<>b.city;

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

Sample table: customers


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 | 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 create the table and insert into it:


CREATE TABLE orders (
ord_no INT PRIMARY KEY,
purch_amt DECIMAL(10, 2),
ord_date DATE,
customer_id INT,
salesman_id INT
);

INSERT INTO orders (ord_no, purch_amt, ord_date, customer_id, salesman_id)


VALUES
(70001, 150.50, '2012-10-05', 3005, 5002),
(70009, 270.65, '2012-09-10', 3001, 5005),
(70002, 65.26, '2012-10-05', 3002, 5001),
(70004, 110.50, '2012-08-17', 3009, 5003),
(70007, 948.50, '2012-09-10', 3005, 5002),
(70005, 2400.60, '2012-07-27', 3007, 5001),
(70008, 5760.00, '2012-09-10', 3002, 5001),
(70010, 1983.43, '2012-10-10', 3004, 5006),
(70003, 2480.40, '2012-10-10', 3009, 5003),
(70012, 250.45, '2012-06-27', 3008, 5002),
(70011, 75.29, '2012-08-17', 3003, 5007),
(70013, 3045.60, '2012-04-25', 3002, 5001);

CREATE TABLE customers (


customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT
);

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);

CREATE TABLE salesman (


salesman_id INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 2)
);

INSERT INTO salesman (salesman_id, name, city, commission)


VALUES
(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 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;

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

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 create the tables and insert into it:
CREATE TABLE customer (
customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT
);

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', 100, 5005);

CREATE TABLE salesman (


salesman_id INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(50),
commission DECIMAL(5, 2)
);
INSERT INTO salesman (salesman_id, name, city, commission)
VALUES
(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 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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy