0% found this document useful (0 votes)
34 views5 pages

Ex 5

The document provides instructions and sample SQL queries to explore joins between tables in a sample database. It includes examples of inner, outer, and equi joins to retrieve customer and salesperson data based on various conditions like commission amounts and cities.

Uploaded by

RAMALAKSHMI K
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)
34 views5 pages

Ex 5

The document provides instructions and sample SQL queries to explore joins between tables in a sample database. It includes examples of inner, outer, and equi joins to retrieve customer and salesperson data based on various conditions like commission amounts and cities.

Uploaded by

RAMALAKSHMI K
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/ 5

RAMALAKSHMI K

953622104080
EXERCISE 5

Query the database tables and explore natural, equi and outer joins
1.From the following tables write a SQL query to find the salesperson(s) and the customer(s) he
represents. Return Customer Name, city, Salesman, commission.

2. From the following tables write a SQL query to find salespeople who received commissions of
more than 12 percent from the company. Return Customer Name, customer city, Salesman,
commission.

3. From the following tables write a SQL query to locate those salespeople who do not live in the
same city where their customers live and have received a commission of more than 12% from the
company. Return Customer Name, customer city, Salesman, salesman city, commission.

4. From the following tables write a SQL query to display the customer name, customer city, grade,
salesman, salesman city. The results should be sorted by ascending customer_id.

5. From the following tables write a SQL query to find those customers with a grade less than 300.
Return cust_name, customer city, grade, Salesman, salesmancity. The result should be ordered by
ascendin customer_id.

6. Write a SQL statement to generate a list in ascending order of salespersons who work either for one
or more customers or have not yet joined any of the customers.

create database ex5

CREATE TABLE SalesMan (


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

INSERT INTO SalesMan (salesman_id, name, city, commission) VALUES


(5001, 'James', 'Hoog New York', 0.15),
(5002, 'NailKnite', 'Paris', 0.13),
(5005, 'Pit Alex', 'London', 0.11),
(5006, 'Mc Lyon', 'Paris', 0.14),
(5003, 'Lauson Hen', 'San Jose', 0.12),
(5007, 'Paul Adam', 'Rome', 0.13);

select * from SalesMan


RAMALAKSHMI K
953622104080

CREATE TABLE Orders (


ord_no INT PRIMARY KEY,
purch_amt DECIMAL(8, 2),
ord_date DATE,
customer_id INT,
salesman_id INT,
FOREIGN KEY (salesman_id) REFERENCES SalesMan(salesman_id)
);
exec sp_help Orders

INSERT INTO Orders (ord_no, purch_amt, ord_date, customer_id, salesman_id) VALUES


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

select * from Orders


RAMALAKSHMI K
953622104080

CREATE TABLE Customers (


customer_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city VARCHAR(50),
grade INT,
salesman_id INT,
FOREIGN KEY (salesman_id) REFERENCES SalesMan(salesman_id)
);
exec sp_help Customers

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

select * from Customers


RAMALAKSHMI K
953622104080

1.SELECT c.cust_name AS "Customer Name", c.city AS "Customer City", s.name AS "Salesman",


s.city AS "Salesman City", s.commission AS "Commission"
FROM Customers c JOIN SalesMan s ON c.salesman_id = s.salesman_id;

2.SELECT c.cust_name AS "Customer Name", c.city AS "Customer City", s.name AS "Salesman",


s.commission AS "Commission"
FROM Customers c JOIN SalesMan s ON c.salesman_id = s.salesman_id WHERE s.commission >
0.12;

3.SELECT c.cust_name AS "Customer Name", c.city AS "Customer City", s.name AS "Salesman",


s.city AS "Salesman City", s.commission AS "Commission"
FROM Customers c JOIN SalesMan s ON c.salesman_id = s.salesman_id WHERE c.city <> s.city
AND s.commission > 0.12;

4.SELECT c.cust_name AS "Customer Name", c.city AS "Customer City", c.grade AS "Grade",


s.name AS "Salesman", s.city AS "Salesman City"
FROM Customers c JOIN SalesMan s ON c.salesman_id = s.salesman_id ORDER BY c.customer_id
ASC;
RAMALAKSHMI K
953622104080

5.SELECT c.cust_name AS "Customer Name", c.city AS "Customer City", c.grade AS "Grade",


s.name AS "Salesman", s.city AS "Salesman City"
FROM Customers c JOIN SalesMan s ON c.salesman_id = s.salesman_id WHERE c.grade < 300
ORDER BY c.customer_id ASC;

6.SELECT s.name AS "Salesman" FROM SalesMan s LEFT JOIN Customers c ON s.salesman_id =


c.salesman_id WHERE c.salesman_id IS NOT NULL
GROUP BY s.name UNION SELECT s.name AS "Salesman" FROM SalesMan s
WHERE NOT EXISTS (SELECT 1 FROM Customers c WHERE c.salesman_id = s.salesman_id)
ORDER BY "Salesman" ASC;

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