Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

1.

From the following tables write a SQL query to find the salesperson and customer who reside in the same city.
Return Salesman, cust_name and city.

SELECT salesman.name AS "Salesman",customer.cust_name, customer.city FROM


salesman,customer WHERE salesman.city=customer.city;

2. 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.

SELECT a.ord_no,a.purch_amt,b.cust_name,b.city FROM orders a, customer b WHERE


a.customer_id=b.customer_i AND a.purch_amt BETWEEN 500 AND 2000;
3. 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

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;
4. 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.

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;
5. 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.

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;
6. From the following tables write a SQL query to find the details of an order. Return ord_no, ord_date,
purch_amt, Customer Name, grade, Salesman, commission.

SELECT a.ord_no,a.ord_date,a.purch_amt,
b.cust_name AS "Customer Name", b.grade,
c.name AS "Salesman", c.commission
FROM orders a
INNER JOIN customer b
ON a.customer_id=b.customer_id
INNER JOIN salesman c
ON a.salesman_id=c.salesman_id;
7. Write a SQL statement to join the tables salesman, customer and orders so that the same column of each
table appears once and only the relational rows are returned.
SELECT *
FROM orders
NATURAL JOIN customer
NATURAL JOIN salesman;
8. 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.

SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",b.city
FROM customer a
LEFT JOIN salesman b
ON a.salesman_id=b.salesman_id
order by a.customer_id;
9. 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 ascending
customer_id.

SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman", b.city
FROM customer a
LEFT OUTER JOIN salesman b
ON a.salesman_id=b.salesman_id
WHERE a.grade<300
ORDER BY a.customer_id;
10. Write a SQL statement to make a report with customer name, city, order number, order date, and order
amount in ascending order according to the order date to determine whether any of the existing customers
have placed an order or not.

SELECT a.cust_name,a.city, b.ord_no,


b.ord_date,b.purch_amt AS "Order Amount"
FROM customer a
LEFT OUTER JOIN orders b
ON a.customer_id=b.customer_id
order by b.ord_date;
11. SQL statement to generate a report with customer name, city, order number, order date, order amount,
salesperson name, and commission to determine if any of the existing customers have not placed orders or if
they have placed orders through their salesman or by themselves.

SELECT a.cust_name,a.city, b.ord_no,


b.ord_date,b.purch_amt AS "Order Amount",
c.name,c.commission
FROM customer a
LEFT OUTER JOIN orders b
ON a.customer_id=b.customer_id
LEFT OUTER JOIN salesman c
ON c.salesman_id=b.salesman_id;

12. 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
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman", b.city
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
ORDER BY b.salesman_id;
13. From the following tables write a SQL query to list all salespersons along with customer name, city, grade,
order number, date, and amount.

SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",
c.ord_no, c.ord_date, c.purch_amt
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
RIGHT OUTER JOIN orders c
ON c.customer_id=a.customer_id;
14. Write a SQL statement to make a list for the salesmen who either work for one or more customers or yet to
join any of the customer. The customer may have placed, either one or more orders on or above order
amount 2000 and must have a grade, or he may not have placed any order to the associated supplier.

SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",
c.ord_no, c.ord_date, c.purch_amt
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
LEFT OUTER JOIN orders c
ON c.customer_id=a.customer_id
WHERE c.purch_amt>=2000
AND a.grade IS NOT NULL;
15. Write a SQL statement to generate a list of all the salesmen who either work for one or more customers or
have yet to join any of them. The customer may have placed one or more orders at or above order amount
2000, and must have a grade, or he may not have placed any orders to the associated supplier.

SELECT a.cust_name,a.city, b.ord_no,


b.ord_date,b.purch_amt AS "Order Amount"
FROM customer a
LEFT OUTER JOIN orders b
ON a.customer_id=b.customer_id;

16. Write a SQL statement to generate a report with the customer name, city, order no. order date, purchase
amount for only those customers on the list who must have a grade and placed one or more orders or which
order(s) have been placed by the customer who neither is on the list nor has a grade.

SELECT a.cust_name,a.city, b.ord_no,


b.ord_date,b.purch_amt AS "Order Amount"
FROM customer a
FULL OUTER JOIN orders b
ON a.customer_id=b.customer_id
WHERE a.grade IS NOT NULL;
17. Write a SQL query to combine each row of the salesman table with each row of the customer table.

SELECT *
FROM salesman a
CROSS JOIN customer b;
18. Write a SQL statement to create a Cartesian product between salesperson and customer, i.e. each salesperson
will appear for all customers and vice versa for that salesperson who belongs to that city.

SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL;
19. Write a SQL statement to create a Cartesian product between salesperson and customer, i.e. each salesperson
will appear for every customer and vice versa for those salesmen who belong to a city and customers who
require a grade

SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL
AND b.grade IS NOT NULL;
20. Write a SQL statement to make a Cartesian product between salesman and customer i.e. each salesman will
appear for all customers and vice versa for those salesmen who must belong to a city which is not the same as
his customer and the customers should have their own grade.

SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL
AND b.grade IS NOT NULL
AND a.city<>b.city;
21. From the following tables write a SQL query to select all rows from both participating tables as long as there is
a match between pro_com and com_id.

SELECT *
FROM item_mast
INNER JOIN company_mast
ON item_mast.pro_com= company_mast.com_id;
22. Write a SQL query to display the item name, price, and company name of all the products.

SELECT item_mast.pro_name, pro_price, company_mast.com_name


FROM item_mast
INNER JOIN company_mast
ON item_mast.pro_com = company_mast.com_id;
23. From the following tables write a SQL query to calculate the average price of items of each company. Return
average value and company name.

SELECT AVG(pro_price), company_mast.com_name


FROM item_mast INNER
JOIN company_mast
ON item_mast.pro_com= company_mast.com_id
GROUP BY company_mast.com_name;
24. From the following tables write a SQL query to calculate and find the average price of items of each company
higher than or equal to Rs. 350. Return average value and company name.

SELECT AVG(pro_price), company_mast.com_name


FROM item_mast INNER JOIN company_mast
ON item_mast.pro_com= company_mast.com_id
GROUP BY company_mast.com_name
HAVING AVG(pro_price) >= 350;
25. From the following tables write a SQL query to find the most expensive product of each company. Return
pro_name, pro_price and com_name.

SELECT A.pro_name, A.pro_price, F.com_name


FROM item_mast A INNER JOIN company_mast F
ON A.pro_com = F.com_id
AND A.pro_price =
(
SELECT MAX(A.pro_price)
FROM item_mast A
WHERE A.pro_com = F.com_id
);

26. From the following tables write a SQL query to display all the data of employees including their department.

SELECT emp_idno, A.emp_fname AS "First Name", emp_lname AS "Last Name",


B.dpt_name AS "Department", emp_dept, dpt_code, dpt_allotment
FROM emp_details A
INNER JOIN emp_department B
ON A.emp_dept = B.dpt_code;
27. From the following tables write a SQL query to display the first and last names of each employee, as well as
the department name and sanction amount.

SELECT emp_details.emp_fname AS "First Name", emp_lname AS "Last Name",


emp_department.dpt_name AS "Department",
dpt_allotment AS "Amount Allotted"
FROM emp_details
INNER JOIN emp_department
ON emp_details.emp_dept = emp_department.dpt_code;

28. From the following tables write a SQL query to find the departments with budgets more than Rs. 50000 and
display the first name and last name of employees

SELECT emp_details.emp_fname AS "First Name", emp_lname AS "Last Name"


FROM emp_details
INNER JOIN emp_department
ON emp_details.emp_dept = emp_department.dpt_code
AND emp_department.dpt_allotment > 50000;
29. From the following tables write a SQL query to find the names of departments where more than two
employees are employed. Return dpt_name.

SELECT emp_department.dpt_name
FROM emp_details
INNER JOIN emp_department
ON emp_dept =dpt_code
GROUP BY emp_department.dpt_name
HAVING COUNT(*) > 2;
SQL SUBQUERIES
1. From the following tables, write a SQL query to find all the orders issued by the salesman 'Paul Adam'.
Return ord_no, purch_amt, ord_date, customer_id and salesman_id

SELECT *
FROM orders
WHERE salesman_id =
(SELECT salesman_id
FROM salesman
WHERE name='Paul Adam');
2. From the following tables write a SQL query to find all orders generated by London-based salespeople.
Return ord_no, purch_amt, ord_date, customer_id, salesman_id

SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city='London');
3. From the following tables write a SQL query to find all orders generated by the salespeople who may work
for customers whose id is 3007. Return ord_no, purch_amt, ord_date, customer_id, salesman_id

SELECT *
FROM orders
WHERE salesman_id =
(SELECT DISTINCT salesman_id
FROM orders
WHERE customer_id =3007);
4. From the following tables write a SQL query to find the order values greater than the average order value
of 10th October 2012. Return ord_no, purch_amt, ord_date, customer_id, salesman_id

SELECT *
FROM orders
WHERE purch_amt >
(SELECT AVG(purch_amt)
FROM orders
WHERE ord_date ='10/10/2012');
5. From the following tables, write a SQL query to find all the orders generated in New York city. Return
ord_no, purch_amt, ord_date, customer_id and salesman_id

SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city ='New York');
6. From the following tables write a SQL query to determine the commission of the salespeople in Paris.
Return commission

SELECT commission
FROM salesman
WHERE salesman_id IN
(SELECT salesman_id
FROM customer
WHERE city = 'Paris');
7. Write a query to display all the customers whose ID is 2001 below the salesperson ID of Mc Lyon

SELECT *
FROM customer
WHERE customer_id =
(SELECT salesman_id -2001
FROM salesman
WHERE name = 'Mc Lyon');
8. From the following tables write a SQL query to count the number of customers with grades above the
average in New York City. Return grade and count

SELECT grade, COUNT (*)


FROM customer
GROUP BY grade
HAVING grade >
(SELECT AVG(grade)
FROM customer
WHERE city = 'New York');
9. From the following tables, write a SQL query to find those salespeople who earned the maximum
commission. Return ord_no, purch_amt, ord_date, and salesman_id

SELECT ord_no, purch_amt, ord_date, salesman_id


FROM orders
WHERE salesman_id IN(
SELECT salesman_id
FROM salesman
WHERE commission = (
SELECT MAX(commission)
FROM salesman));
10. From the following tables write SQL query to find the customers who placed orders on 17th August 2012.
Return ord_no, purch_amt, ord_date, customer_id, salesman_id and cust_name

SELECT b.*, a.cust_name


FROM orders b, customer a
WHERE a.customer_id=b.customer_id
AND b.ord_date='2012-08-17';
11. From the following tables write a SQL query to find salespeople who had more than one customer. Return
salesman_id and name

SELECT salesman_id,name
FROM salesman a
WHERE 1 <
(SELECT COUNT(*)
FROM customer
WHERE salesman_id=a.salesman_id);
12. From the following tables write a SQL query to find those orders, which are higher than the average
amount of the orders. Return ord_no, purch_amt, ord_date, customer_id and salesman_id

SELECT *
FROM orders a
WHERE purch_amt >
(SELECT AVG(purch_amt) FROM orders b
WHERE b.customer_id = a.customer_id);
13. From the following tables write a SQL query to find those orders that are equal or higher than the average
amount of the orders. Return ord_no, purch_amt, ord_date, customer_id and salesman_id

SELECT *
FROM orders a
WHERE purch_amt >=
(SELECT AVG(purch_amt) FROM orders b
WHERE b.customer_id = a.customer_id);
14. Write a query to find the sums of the amounts from the orders table, grouped by date, and eliminate all
dates where the sum was not at least 1000.00 above the maximum order amount for that date

SELECT ord_date, SUM (purch_amt)


FROM orders a
GROUP BY ord_date
HAVING SUM (purch_amt) >
(SELECT 1000.00 + MAX(purch_amt)
FROM orders b

WHERE a.ord_date = b.ord_date);

Q1.  Write an SQL query to fetch the current date-time from the system.
TO fetch the CURRENT DATE IN SQL Server

 SELECT GETDATE();

Q2. Write a SQL query to fetch the PatientName in uppercase and state as lowercase. Also
use the ALIAS name for the result-set as PatName and NewState.
 SELECT upper(patientname) as PatName, Lower(State) as Newstate from Table

Q3. Find the Nth highest consultation fees from the PatientsCheckup table with and without
using the TOP/LIMIT keywords.
Nth highest consultation fees from the PatientsCheckup table with using the TOP keywords

SELECT TOP 1 ConsultationFees

FROM(

SELECT TOP N ConsultationFees

FROM PatientsCheckup

ORDER BY ConsultationFees DESC) AS FEES

ORDER BY ConsultationFees ASC;

Nth highest consultation fees from the PatientsCheckup table without using the TOP/LIMIT keywords.

SELECT ConsultationFees

FROM PatientsCheckup F1

WHERE N-1 = (

SELECT COUNT( DISTINCT ( F2.ConsultationFees ) )

FROM PatientsCheckup F2

WHERE F2.ConsultationFees > F1.ConsultationFees );

Q4. Write a query to fetch top N records using the TOP/LIMIT, ordered by
ConsultationFees.

TOP Command – SQL Server

SELECT TOP N * FROM PatientsCheckup ORDER BY ConsultationFees DESC;

LIMIT Command - MySQL

SELECT * FROM PatientsCheckup ORDER BY ConsultationFees DESC LIMIT N;

Q5. Write a SQL query to create a table where the structure is copied from other table.

Create a table consisting of data

-USING SELECT command

SELECT * INTO NewPatientsTable FROM Patients WHERE 1 = 0;


Copy code

– USING CREATE command IN MySQL

CREATE TABLE NewPatientsTable AS SELECT * FROM Patients;


Copy code

Q6. Write a query to fetch even and odd rows from a table.

Fetch even rows in SQL Server

SELECT * FROM (

SELECT *, ROW_NUMBER() OVER(ORDER BY PatientId) AS RowNumber

FROM Patients

) P

WHERE P.RowNumber % 2 = 0; for odd= %2=1;

Q7. Write an SQL query to fetch duplicate records from Patients, without considering the
primary key.
SELECT PatientName, DoctorID, RegDate, State, COUNT(*)

FROM Patients

GROUP BY PatientName, DoctorID, RegDate, State

HAVING COUNT(*) > 1;

Q8. Write a query to fetch the number of patients whose weight is greater than 68.
SELECT COUNT(*) FROM PatientsCheckup WHERE Weight > '68';

Q9. Write a query to retrieve the list of patients from the same state.
SELECT DISTINCT P.PatientID, P.PatientName, P.State

FROM Patients P, Patient P1

WHERE P.State = P1.State AND P.PatientID != P1.PatientID;

Q10. Write a query to retrieve two minimum and maximum consultation fees from the
PatientsCheckup Table.

[code]

– TWO MINIMUM CONSULTATION FEES

SELECT DISTINCT ConsultationFees FROM PatientsCheckup P1

WHERE 2 >= (SELECT COUNT(DISTINCT ConsultationFees)FROM PatientsCheckup P2


WHERE P1.ConsultationFees >= P2.ConsultationFees) ORDER BY P1.SConsultationFees DESC;

– TWO MAXIMUM CONSULTATION FEES

SELECT DISTINCT ConsultationFees FROM PatientsCheckup P1

WHERE 2 >= (SELECT COUNT(DISTINCT ConsultationFees)FROM PatientsCheckup P2

WHERE P1.ConsultationFees <= P2.ConsultationFees) ORDER BY P1.ConsultationFees DESC;

[/code]

Copy code

Q11. Write a query to fetch patient details along with the weight fees, even if the details are
missing.
SELECT P.PatientName, C.ConsultationFees

FROM Patients P

LEFT JOIN

PatientsCheckup C

ON P.PatientId = C.PatientId;

Q12. Write a SQL query to fetch doctor wise count of patients sorted by the doctors.
SELECT DoctorID, COUNT(PatientID) AS DocPat

FROM Patients GROUP BY DoctorID

ORDER BY DocPat;

Q13. Write a SQL query to fetch the first and last record of the Patients table.
–FETCH FIRST RECORD

SELECT * FROM Patients WHERE PatientID = (SELECT MIN(PatientID) FROM


Patients);

–FETCH LAST RECORD

SELECT * FROM Patients WHERE PatientID = (SELECT MAX(PatientID) FROM


Patients);
Q14. Write a SQL query to fetch consultation fees – wise count and sort them in descending
order.
SELECT ConsultationFees, COUNT(PatientId) CFCount

FROM PatientsCheckup

GROUP BY ConsultationFees

ORDER BY CFCount DESC;

Q15. Write a SQL query to retrieve patient details from the Patients table who have a weight
in the PatientsCheckup table.
SELECT * FROM Patients P

WHERE EXISTS

(SELECT * FROM PatientsCheckup C WHERE P.PatientID = C.PatientID);

Q16. Write a SQL query to retrieve the last 2 records from the Patients table.
SELECT * FROM Patients WHERE

PatientID <=2 UNION SELECT * FROM

(SELECT * FROM Patients P ORDER BY P.PatientID DESC)

AS P1 WHERE P1.PatientID <=2;

Q17. Write a SQL query  to find all the patients who joined in the year 2022.
–USING BETWEEN

SELECT * FROM Patients

WHERE RegDate BETWEEN '2021/01/01' AND '2021/12/31';

– USING YEAR

SELECT * FROM Patients WHERE YEAR(RegDate ) = '2021'; 

Q18. Write a SQL query to fetch 50% records from the PatientsCheckup table.
SELECT *

FROM PatientsCheckup WHERE

PatientID <= (SELECT COUNT(PatientD)/2 FROM PatientsCheckup);

Q19. Write a query to find those patients who have paid consultation fees between 400 to
700.
SELECT * FROM Patients WHERE PatientID IN

(SELECT PatientID FROM PatientsCheckup WHERE ConsultationFees BETWEEN '400' AND '700');
Copy code

Q20. Write a query to update the patient names by removing the leading and trailing spaces.
UPDATE Patients

SET PatientName = LTRIM(RTRIM(PatientName));

Q21. Write a query to add email validation to your database.


SELECT email FROM Patients WHERE NOT REGEXP_LIKE(email, ‘[A-Z0-9._%+-]+@[A-Z0-
9.-]+.[A-Z]{2,4}’, ‘i’);

Q22. Write a query to find all patient names whose name:

 Begin with A
 Ends with S and contains 3 alphabets
 Staying in the state Telangana

SELECT * FROM Patients WHERE PatientName LIKE 'A%';

SELECT * FROM Patients WHERE PatientName LIKE '___S';

SELECT * FROM Patients WHERE State LIKE 'Telangana%’;

Q23. Write a SQL query to fetch details of all patients excluding patients with name 
“Sheela” and “Anay”.
SELECT * FROM Patients WHERE PatientName NOT IN ('Sheela','Anay'); 

Q24. Write a query to fetch the total count of occurrences of a particular character – ‘x’ in
the PatientName.
SELECT PatientName, PatientID

LENGTH(PatientName) - LENGTH(REPLACE(PatientName, 'x', ''))

FROM Patients;

Q25. Write a query to retrieve the first three characters of  PatientName from the Patients
table.
SELECT SUBSTRING(PatientName, 1, 3) FROM Patients; 

Q26. Write a query to fetch only the Address (string before space).
 
USING SUBSTRING

SELECT SUBSTRING(Address, 1, CHARINDEX(' ',Address)) FROM Patients;

Q27. Write a query to combine Address and state into a new column – NewAddress.
SELECT CONCAT(Address, ' ', State) AS 'NewAddress' FROM Patients; 

Q28. Write a query to fetch PatientIDs  which are present in: 

 Both tables
 One of the table. Let us say, patients present in Patients and not in the PatientsCheckup table.

–Present IN BOTH TABLES

SELECT PatientId FROM Patients

WHERE PatientId IN

(SELECT PatientId FROM PatientsCheckup);

– Present IN One OF the TABLE

SELECT PatientId FROM Patients

WHERE PatientId NOT IN

(SELECT PatientId FROM PatientsCheckup);

Q29. Write a query to find the number of patients whose RegDate is between 01/04/2021 to
31/12/2022 and are grouped according to state.
SELECT COUNT(*), State FROM Patients WHERE RegDate BETWEEN '01/04/2021' AND
'31/12/2022' GROUP BY State;

Q30. Write a query to fetch all records from the Patients table; ordered by PatientName in
ascending order, State in descending order.
SELECT * FROM Patients ORDER BY PatientName ASC, State DESC;

You might also like