0% found this document useful (0 votes)
28 views8 pages

Prashant Tripathi (Ajay Kumar Garg Engineering College) SQL Assignemnt

Ajay Kumar Garg Engineering College

Uploaded by

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

Prashant Tripathi (Ajay Kumar Garg Engineering College) SQL Assignemnt

Ajay Kumar Garg Engineering College

Uploaded by

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

Section 1: Select Queries

a) Sample table: salesman

1. Display specific columns like name and commission for all salesmen

SELECT name, commission FROM salesman;

2. Display salesman_id, name, city, and commission within the range more than 0.10%
and less than 0.12%.

SELECT salesman_id, name, city, commission


FROM salesman
WHERE commission > 0.10 AND commission < 0.12;

3. Produce a list of salesman_id, name, city, and commission of each salesman who lives
in cities other than Paris and Rome.

SELECT salesman_id, name, city, commission


FROM salesman
WHERE city NOT IN ('Paris', 'Rome');

4. Find salesmen with all other information and names starting with any letter within 'A'
and 'L' (not inclusive).

SELECT *
FROM salesman
WHERE name >= 'A' AND name < 'L';

5. Find salesmen with all other information and names starting with a letter other than
and 'L' (not inclusive).

SELECT *
FROM salesman
WHERE name < 'A' OR name >= 'L';

6. Find a customer with all information whose name begins with the letter 'B'.

SELECT *
FROM salesman
WHERE name LIKE 'B%';
b) Sample table: orders

1. Write a query to display the columns in a specific order like order date, salesman id,
order number and purchase amount from for all the orders.

SELECT ord_date, salesman_id, ord_no, purch_amt


FROM orders;

2. Write a query to display distinct SalesName Name who is receiving the orders.

SELECT DISTINCT s.name AS Salesman_Name


FROM salesman s
JOIN orders o ON s.salesman_id = o.salesman_id;

3. Write a SQL statement to display names and commission of salesman, who belongs to
the city of Paris.

SELECT s.name, s.commission


FROM salesman s
WHERE s.city = 'Paris';

4. Write a SQL statement to display either those orders which are not issued on date
2012-09-10 and issued by the salesman whose ID is 5005 and below or those orders
which purchase amount is 1000.00 and below.

SELECT *
FROM orders
WHERE (ord_date != '2012-09-10' AND salesman_id <= 5005) OR purch_amt
<=1000.00;

5. Write a SQL query to display order number, purchase amount, the achieved and
unachieved percentage (%) for those order which exceeds the 50% of the target value
of 6000.

SELECT ord_no, purch_amt,


CASE WHEN purch_amt > 3000 THEN 'Achieved' ELSE 'Unachieved' END AS
Achievement_Status, ((purch_amt / 6000) * 100) AS Percentage_Achieved
FROM orders
WHERE (purch_amt / 6000) * 100 > 50;
c) Sample table: customer

1. Write a SQL statement to display all the information for those customers with a grade of 200.

SELECT *
FROM customer
WHERE grade = 200;

2. Write a SQL statement to display all the information for those customers with a grade between
200 and 300.

SELECT *
FROM customer
WHERE grade BETWEEN 200 AND 300;

3. Write a query statement to display all customers in New York who have a grade value above 100.

SELECT *
FROM customer
WHERE city = 'New York' AND grade > 100;

4. Write a SQL statement to display all customers, who are either belongs to the city New York or had
a grade above 100.

SELECT *
FROM customer
WHERE city = 'New York' OR grade > 100;

5. Write a SQL statement to display all the customers, who are either belongs to the city New York or
not had a grade above 100.

SELECT *
FROM customer
WHERE city = 'New York' OR grade <= 100;

6. Write a SQL query to display those customers who are neither belongs to the city New York nor
grade value is more than 100.

SELECT *
FROM customer
WHERE city != 'New York' AND grade <= 100;

7. Write a query to sort out those customers with all information whose ID value is within any of
3007, 3008 and 3009.
SELECT *
FROM customer
WHERE customer_id IN (3007, 3008, 3009);
8. Write a SQL statement to find all those customers with all information whose names are ending
with the letter 'n'.

SELECT *
FROM customer
WHERE cust_name LIKE '%n';

9. Write a SQL statement to find that customer with all information who gets a grade except NULL
value.
SELECT *
FROM customer
WHERE grade IS NOT NULL;

10. Write a SQL statement know how many customers listed.

SELECT COUNT(*) AS Customer_Count


FROM customer;

11. Write a SQL statement find the number of customers who has a Grade.

SELECT COUNT(*) AS Customer_With_Grade_Count


FROM customer
WHERE grade IS NOT NULL;

12. Write a SQL statement which selects the highest grade for each of the cities.

SELECT city, MAX(grade) AS highest_grade


FROM customer
GROUP BY city;
Section 2: Join Queries

1. Write a SQL statement to find the names of all customers along with the salesmen who works for
them.
SELECT c.cust_name, s.name AS salesman_name
FROM customer c
JOIN salesman s ON c.salesman_id = s.salesman_id;

2. Write a SQL statement to display all those orders by the customers not located in the same cities.

SELECT o.*
FROM orders o
JOIN customer c ON o.customer_id = c.customer_id
JOIN salesman s ON o.salesman_id = s.salesman_id
WHERE c.city <> s.city;

3. Write a SQL statement to display Customer Name, SalesName, Sum of Purchase Amount.

SELECT c.cust_name, s.name AS salesman_name, SUM(o.purch_amt) AS


total_purchase_amount
FROM customer c
JOIN orders o ON c.customer_id = o.customer_id
JOIN salesman s ON o.salesman_id = s.salesman_id
GROUP BY c.cust_name, s.name;

4. Write a query that produces all customers with their name, city, salesman and commission, who
served by a salesman and the salesman works at a rate of the commission within 12% to 14%.

SELECT c.*, s.name AS salesman_name, s.commission


FROM customer c
JOIN salesman s ON c.salesman_id = s.salesman_id
WHERE s.commission BETWEEN 0.12 AND 0.14;

5. Find all customer with orders on October 5, 2012.

SELECT c.*, o.*


FROM customer c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.ord_date = '2012-10-05';

6. Write a SQL statement to prepare a list with salesman name, customer name and their cities for the
salesmen and customer who belongs to the same city.

SELECT s.name AS salesman_name, c.cust_name, c.city


FROM salesman s
JOIN customer c ON s.city = c.city;
Section 3: ORDER BY/GROUP BY Queries

1. Write a SQL statement to find the total purchase amount of all orders.

SELECT SUM(purch_amt) AS total_purchase_amount

FROM orders;

2. Write a SQL statement to find the average purchase amount of all orders.

SELECT AVG(purch_amt) AS average_purchase_amount


FROM orders;

3. Write a SQL statement to find the number of salesman currently listing for all of their customers.

SELECT salesman_id, COUNT(DISTINCT customer_id) AS num_customers

FROM orders

GROUP BY salesman_id;

4. Write a SQL statement to get the maximum purchase amount of all the orders.

SELECT MAX(purch_amt) AS max_purchase_amount


FROM orders;

5. Write a SQL statement to get the minimum purchase amount of all the orders.

SELECT MIN(purch_amt) AS min_purchase_amount


FROM orders;

6. Write a SQL statement to find the highest purchase amount ordered by the each customer with their ID

and highest purchase amount.

SELECT customer_id, MAX(purch_amt) AS highest_purchase_amount

FROM orders

GROUP BY customer_id;

7. Write a SQL statement to find the highest purchase amount ordered by the each customer on a

particular date with their ID, order date and highest purchase amount.

SELECT customer_id, ord_date, MAX(purch_amt) AS highest_purchase_amount

FROM orders

GROUP BY customer_id, ord_date;


8. Write a SQL statement to find the highest purchase amount with their ID and order date, for only those

customers who have highest purchase amount in a day is more than 2000.

SELECT customer_id, ord_date, MAX(purch_amt) AS highest_purchase_amount

FROM orders

GROUP BY customer_id, ord_date

HAVING MAX(purch_amt) > 2000;

9. Write a SQL statement to find the highest purchase amount with their ID, for only those customers

whose ID is within the range 3002 and 3007.

SELECT customer_id, MAX(purch_amt) AS highest_purchase_amount

FROM orders

WHERE customer_id BETWEEN 3002 AND 3007

GROUP BY customer_id;

10. Write a SQL statement to display customer details (ID and max purchase amount) whose IDs are

within the range 3002 and 3007 and highest purchase amount is more than 1000.

SELECT customer_id, MAX(purch_amt) AS highest_purchase_amount

FROM orders

WHERE customer_id BETWEEN 3002 AND 3007

GROUP BY customer_id

HAVING MAX(purch_amt) > 1000;

11. Write a SQL statement to find the highest purchase amount with their ID, for only those salesmen

whose ID is within the range 5003 and 5008.

SELECT salesman_id, MAX(purch_amt) AS highest_purchase_amount

FROM orders

WHERE salesman_id BETWEEN 5003 AND 5008

GROUP BY salesman_id;

12. Write a SQL statement that counts all orders for a date August 17th, 2012.

SELECT COUNT(*) AS num_orders

FROM orders

WHERE ord_date = '2012-08-17';


Section 4: Sub Query

1. Write a query in SQL to display the ID for those employees who did two or more jobs in the past.

SELECT employee_id
FROM job_history
GROUP BY employee_id
HAVING COUNT(DISTINCT job_id) >= 2;

2. Write a query to display all salesmen and customer located in London.

SELECT *

FROM salesman

WHERE city = 'London'

UNION

SELECT *

FROM customer

WHERE city = 'London';

You might also like