Adding SQL query language

master
Michael Reber 4 years ago
parent 026079a47d
commit 8c9a19ec97

@ -0,0 +1,3 @@
/*Write a SQL query to calculate the average price of all the products.*/
SELECT AVG(pro_price) AS "Average Price"
FROM item_mast;

@ -0,0 +1,5 @@
/*Write a SQL query to display the average price of each company's products, along with their code.*/
SELECT AVG(pro_price) AS "Average Price",
pro_com AS "Company ID"
FROM item_mast
OUP BY pro_com;

@ -0,0 +1,4 @@
/*Write a SQL query to find the number of products with a price more than or equal to Rs.350.*/
SELECT COUNT(*) AS "Number of Products"
FROM item_mast
WHERE pro_price >= 350;

@ -0,0 +1,3 @@
/*Write a SQL statement find the number of customers who gets at least a gradation for his/her performance.*/
SELECT COUNT (ALL grade)
FROM customer;

@ -0,0 +1,4 @@
/*Write a SQL statement that counts all orders for a date August 17th, 2012.*/
SELECT COUNT(*)
FROM orders
WHERE ord_date='2012-08-17';

@ -0,0 +1,4 @@
/*Write a SQL statement that counts the number of different non NULL city values for salesmen.*/
SELECT COUNT(*)
FROM salesman
WHERE city IS NOT NULL;

@ -0,0 +1,7 @@
/*Write a SQL statement to display customer details (ID and 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)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id
HAVING MAX(purch_amt)>1000;

@ -0,0 +1,3 @@
/*Write a SQL statement to find the average purchase amount of all orders.*/
SELECT AVG (purch_amt)
FROM orders;

@ -0,0 +1,5 @@
/*Write a SQL statement to find the highest purchase amount on a date '2012-08-17' for each salesman with their ID.*/
SELECT salesman_id,MAX(purch_amt)
FROM orders
WHERE ord_date = '2012-08-17'
GROUP BY salesman_id;

@ -0,0 +1,5 @@
/*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)
FROM orders
GROUP BY customer_id,ord_date;

@ -0,0 +1,5 @@
/*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)
FROM orders
GROUP BY customer_id;

@ -0,0 +1,6 @@
/*Write a SQL statement to find the highest purchase amount with their ID and order date, for only those customers
who have a higher purchase amount in a day is within the list 2000, 3000, 5760 and 6000.*/
SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt) IN(2000 ,3000,5760, 6000);

@ -0,0 +1,6 @@
/*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)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt)>2000.00;

@ -0,0 +1,6 @@
/*Write a SQL statement to find the highest purchase amount with their ID and order date,
for those customers who have a higher purchase amount in a day is within the range 2000 and 6000.*/
SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;

@ -0,0 +1,6 @@
/*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)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id;

@ -0,0 +1,6 @@
/*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)
FROM orders
GROUP BY salesman_id
HAVING salesman_id BETWEEN 5003 AND 5008;

@ -0,0 +1,3 @@
/*Write a SQL statement to find the number of salesmen currently listing for all of their customers.*/
SELECT COUNT (DISTINCT salesman_id)
FROM orders;

@ -0,0 +1,3 @@
/*Write a SQL statement to find the total purchase amount for all orders.*/
SELECT SUM (purch_amt)
FROM orders;

@ -0,0 +1,3 @@
/*Write a SQL statement to know how many customers have listed their names.*/
SELECT COUNT(*)
FROM customer;

@ -0,0 +1,3 @@
/*Write a SQL statement to know the maximum purchase amount of all the orders.*/
SELECT MAX (purch_amt)
FROM orders;

@ -0,0 +1,3 @@
/*Write a SQL statement to know the minimum purchase amount of all the orders. */
SELECT MIN(purch_amt)
FROM orders;

@ -0,0 +1,4 @@
/*Write a SQL statement which selects the highest grade for each of the cities of the customers.*/
SELECT city,MAX(grade)
FROM customer
GROUP BY city;

@ -0,0 +1,4 @@
/*Write a query in SQL to find the number of employees in each department along with the department code.*/
SELECT emp_dept, COUNT(*)
FROM emp_details
GROUP BY emp_dept;

@ -0,0 +1,3 @@
/*Write a query in SQL to find the sum of the allotment amount of all departments.*/
SELECT SUM(dpt_allotment)
FROM emp_department;

@ -0,0 +1,4 @@
/*Write a query that counts the number of salesmen with their order date and ID registering orders for each day.*/
SELECT ord_date,salesman_id,COUNT(*)
FROM orders
GROUP BY ord_date,salesman_id;

@ -0,0 +1,7 @@
/*Display all in reverse, where order dates equal to a specified date or
customer id greater than a specified number and purchase amount less than a specified amount. */
SELECT *
FROM orders
WHERE NOT((ord_date ='2012-08-17'
OR customer_id>3005)
AND purch_amt<1000);

@ -0,0 +1,7 @@
/*Write a SQL query to display all orders where purchase amount less than a specified amount or order date and customer_id must
not be greater than a specified data and less than a specified ID respectively.*/
SELECT *
FROM orders
WHERE(purch_amt<200 OR
NOT(ord_date>='2012-02-10'
AND customer_id<3009));

@ -0,0 +1,7 @@
/*Write a SQL query to display order number, purchase amount, achived, the unachieved percentage for those order
which exceeds the 50% of the target value of 6000. */
SELECT ord_no,purch_amt,
(100*purch_amt)/6000 AS "Achieved %",
(100*(6000-purch_amt)/6000) AS "Unachieved %"
FROM orders
WHERE (100*purch_amt)/6000>50;

@ -0,0 +1,4 @@
/*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 NOT (city = 'New York' OR grade>100);

@ -0,0 +1,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;

@ -0,0 +1,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 NOT grade>100;

@ -0,0 +1,7 @@
/*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 505 and below or those orders which purchase amount is 1000.00 and below.*/
SELECT *
FROM orders
WHERE NOT ((ord_date ='2012-09-10'
AND salesman_id>505)
OR purch_amt>1000.00);

@ -0,0 +1,6 @@
/*Write a SQL statement to display salesman_id, name, city and commission who gets the
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);

@ -0,0 +1,4 @@
/*Write a query in SQL to display all the data of employees that work in department 47 or department 63.*/
SELECT *
FROM emp_details
WHERE emp_dept = 47 OR emp_dept = 63;

@ -0,0 +1,4 @@
/*Write a query in SQL to find the data of employees whose last name is Dosni or Mardy.*/
SELECT *
FROM emp_details
WHERE emp_lname ='Dosni' OR emp_lname= 'Mardy';

@ -0,0 +1,4 @@
/*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;

@ -0,0 +1,4 @@
/*Write a query to display all customers with a grade above 100.*/
SELECT *
FROM customer
WHERE grade > 100;

@ -0,0 +1,5 @@
/*Write a SQL statement to arrange the orders according to the order_date in such a manner that
the latest date will come first then previous dates.*/
SELECT *
FROM orders
ORDER BY ord_date DESC;

@ -0,0 +1,5 @@
/*Write a SQL statement to display customer name, city and grade in such a manner that,
the customer holding highest grade will come first.*/
SELECT cust_name,city,grade
FROM customer
ORDER BY 3 DESC;

@ -0,0 +1,4 @@
/*Write a SQL statement to display the commission with the percent sign ( % ) with salesman ID,
name and city columns for all the salesmen.*/
SELECT salesman_id,name,city,'%',commission*100
FROM salesman;

@ -0,0 +1,5 @@
/*Write a SQL statement to display the customer name, city, and grade, etc. and the display will
be arranged according to the smallest customer ID.*/
SELECT cust_name,city,grade
FROM customer
ORDER BY customer_id;

@ -0,0 +1,5 @@
/*Write a SQL statement to display the orders with all information in such a manner that, the older order
date will come first and the highest purchase amount of same day will come first.*/
SELECT *
FROM orders
ORDER BY ord_date,purch_amt DESC;

@ -0,0 +1,6 @@
/*Write a SQL statement to find out the number of orders booked for each day and display it in
such a format like "For 2001-10-10 there are 15 orders".*/
SELECT ' For',ord_date,',there are',
COUNT (DISTINCT ord_no),'orders.'
FROM orders
GROUP BY ord_date;

@ -0,0 +1,7 @@
/*Write a SQL statement to make a report with customer ID in such a manner that, the largest number of orders booked by the
customer will come first along with their highest purchase amount.*/
SELECT customer_id, COUNT(DISTINCT ord_no),
MAX(purch_amt)
FROM orders
GROUP BY customer_id
ORDER BY 2 DESC;

@ -0,0 +1,4 @@
/*Write a query to display the orders according to the order number arranged by ascending order.*/
SELECT *
FROM orders
ORDER BY ord_no;

@ -0,0 +1,6 @@
/*1. Write a query in SQL to display the first name, last name, department number, and department name for each employee. */
SELECT E.first_name , E.last_name ,
E.department_id , D.department_name
FROM employees E
JOIN departments D
ON E.department_id = D.department_id;

@ -0,0 +1,6 @@
/*10. Write a query in SQL to display the first name, last name, department number and name, for all employees who have or have
not any department.*/
SELECT E.first_name, E.last_name, E.department_id, D.department_name
FROM employees E
LEFT OUTER JOIN departments D
ON E.department_id = D.department_id;

@ -0,0 +1,7 @@
/*11. Write a query in SQL to display the first name of all employees and the first name of their manager including those who
does not working under any manager.*/
SELECT E.first_name AS "Employee Name",
M.first_name AS "Manager"
FROM employees E
LEFT OUTER JOIN employees M
ON E.manager_id = M.employee_id;

@ -0,0 +1,8 @@
/*13. Write a query in SQL to display the job title, department name, full name (first and last name ) of employee, and
starting date for all the jobs which started on or after 1st January, 1993 and ending with on or before 31 August, 1997.*/
SELECT job_title, department_name, first_name || ' ' || last_name AS Employee_name, start_date
FROM job_history
JOIN jobs USING (job_id)
JOIN departments USING (department_id)
JOIN employees USING (employee_id)
WHERE start_date>='1993-01-01' AND start_date<='1997-08-31';

@ -0,0 +1,6 @@
/*14. Write a query in SQL to display job title, full name (first and last name ) of employee, and the difference between
maximum salary for the job and salary of the employee.*/
SELECT job_title, first_name || ' ' || last_name AS Employee_name,
max_salary-salary AS salary_difference
FROM employees
NATURAL JOIN jobs;

@ -0,0 +1,6 @@
/*15. Write a query in SQL to display the name of the department, average salary and number of employees working in that
department who got commission.*/
SELECT department_name, AVG(salary), COUNT(commission_pct)
FROM departments
JOIN employees USING (department_id)
GROUP BY department_name;

@ -0,0 +1,5 @@
/*17. Write a query in SQL to display the name of the country, city, and the departments which are running there.*/
SELECT country_name,city, department_name
FROM countries
JOIN locations USING (country_id)
JOIN departments USING (location_id);

@ -0,0 +1,5 @@
/*18. Write a query in SQL to display department name and the full name (first and last name) of the manager.*/
SELECT department_name, first_name || ' ' || last_name AS name_of_manager
FROM departments D
JOIN employees E
ON (D.manager_id=E.employee_id);

@ -0,0 +1,5 @@
/*19. Write a query in SQL to display job title and average salary of employees.*/
SELECT job_title, AVG(salary)
FROM employees
NATURAL JOIN jobs
GROUP BY job_title;

@ -0,0 +1,8 @@
/*2. Write a query in SQL to display the first and last name, department, city, and state province for each employee. */
SELECT E.first_name,E.last_name,
D.department_name, L.city, L.state_province
FROM employees E
JOIN departments D
ON E.department_id = D.department_id
JOIN locations L
ON D.location_id = L.location_id;

@ -0,0 +1,7 @@
/*20. Write a query in SQL to display the details of jobs which was done by any of the employees who is presently earning a
salary on and above 12000.*/
SELECT a.*
FROM job_history a
JOIN employees m
ON (a.employee_id = m.employee_id)
WHERE salary >= 12000;

@ -0,0 +1,12 @@
/*21. Write a query in SQL to display the country name, city, and number of those departments where at leaste 2 employees
are working.*/
SELECT country_name,city, COUNT(department_id)
FROM countries
JOIN locations USING (country_id)
JOIN departments USING (location_id)
WHERE department_id IN
(SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(department_id)>=2)
GROUP BY country_name,city;

@ -0,0 +1,6 @@
/*22. Write a query in SQL to display the department name, full name (first and last name) of manager, and their city.*/
SELECT department_name, first_name || ' ' || last_name AS name_of_manager, city
FROM departments D
JOIN employees E
ON (D.manager_id=E.employee_id)
JOIN locations L USING (location_id);

@ -0,0 +1,5 @@
/*23. Write a query in SQL to display the employee ID, job name, number of days worked in for all those jobs in department 80.*/
SELECT employee_id, job_title, end_date-start_date DAYS
FROM job_history
NATURAL JOIN jobs
WHERE department_id=80;

@ -0,0 +1,7 @@
/*24. Write a query in SQL to display the full name (first and last name), and salary of those employees who working in any
department located in London.*/
SELECT first_name || ' ' || last_name AS Employee_name, salary
FROM employees
JOIN departments USING (department_id)
JOIN locations USING (location_id)
WHERE city = 'London';

@ -0,0 +1,9 @@
/*25. Write a query in SQL to display full name(first and last name), job title, starting and ending date of last jobs for
those employees with worked without a commission percentage.*/
SELECT first_name || ' ' || last_name AS Employee_name,
job_title, start_date, end_date
FROM job_history a
JOIN jobs b USING (job_id)
JOIN employees c
ON ( a.employee_id = c.employee_id)
WHERE commission_pct IS NULL;

@ -0,0 +1,5 @@
/*26. Write a query in SQL to display the department name and number of employees in each of the department.*/
SELECT department_name, COUNT(*)
FROM employees
NATURAL JOIN departments
GROUP BY department_name;

@ -0,0 +1,5 @@
/*3. Write a query in SQL to display the first name, last name, salary, and job grade for all employees. */
SELECT E.first_name, E.last_name, E.salary, J.grade_level
FROM employees E
JOIN job_grades J
ON E.salary BETWEEN J.lowest_sal AND J.highest_sal;

@ -0,0 +1,9 @@
/*4. Write a query in SQL to display the first name, last name, department number and department name, for all employees for
departments 80 or 40. */
SELECT E.first_name , E.last_name ,
E.department_id , D.department_name
FROM employees E
JOIN departments D
ON E.department_id = D.department_id
AND E.department_id IN (80 , 40)
ORDER BY E.last_name;

@ -0,0 +1,10 @@
/*5. Write a query in SQL to display those employees who contain a letter z to their first name and also display their last name,
department, city, and state province.*/
SELECT E.first_name,E.last_name,
D.department_name, L.city, L.state_province
FROM employees E
JOIN departments D
ON E.department_id = D.department_id
JOIN locations L
ON D.location_id = L.location_id
WHERE E.first_name LIKE '%z%';

@ -0,0 +1,5 @@
/*6. Write a query in SQL to display all departments including those where does not have any employee.*/
SELECT E.first_name, E.last_name, E.department_id, D.department_name
FROM employees E
RIGHT OUTER JOIN departments D
ON E.department_id = D.department_id;

@ -0,0 +1,7 @@
/*7. Write a query in SQL to display the first and last name and salary for those employees who earn less than the employee
earn whose number is 182.*/
SELECT E.first_name, E.last_name, E.salary
FROM employees E
JOIN employees S
ON E.salary < S.salary
AND S.employee_id = 182;

@ -0,0 +1,6 @@
/*8. Write a query in SQL to display the first name of all employees including the first name of their manager.*/
SELECT E.first_name AS "Employee Name",
M.first_name AS "Manager"
FROM employees E
JOIN employees M
ON E.manager_id = M.employee_id;

@ -0,0 +1,5 @@
/*9. Write a query in SQL to display the department name, city, and state province for each department.*/
SELECT D.department_name , L.city , L.state_province
FROM departments D
JOIN locations L
ON D.location_id = L.location_id;

@ -0,0 +1,6 @@
/*2Write a SQL query to display the average price of items of each company, showing the name of the company.*/
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;

@ -0,0 +1,10 @@
/*3Write a SQL query to display the average price of items of each company, showing the name of the company.*/
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
);

@ -0,0 +1,5 @@
/*Write a SQL query to display all the data from the item_mast, including all the data for each item's producer company.*/
SELECT *
FROM item_mast
INNER JOIN company_mast
ON item_mast.pro_com= company_mast.com_id;

@ -0,0 +1,6 @@
/*Write a SQL query to display the average price of items of each company, showing the name of the company.*/
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;

@ -0,0 +1,5 @@
/*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;

@ -0,0 +1,10 @@
/*Write a SQL statement to find the details of a order i.e. order number, order date, amount of order, which customer gives the
order and which salesman works for that customer and how much commission he gets for an order.*/
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;

@ -0,0 +1,9 @@
/*Write a SQL statement to find the list of customers who appointed a salesman for their jobs who does not live in the same
city where their customer lives, and gets a commission is above 12%.*/
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;

@ -0,0 +1,8 @@
/*Write a SQL statement to find the list of customers who appointed a salesman for their jobs who gets a commission from the
company is more than 12%.*/
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;

@ -0,0 +1,6 @@
/*Write a SQL statement to know which salesman are working for which customer.*/
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;

@ -0,0 +1,6 @@
/*Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all
customer and vice versa for that customer who belongs to a city. */
SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL;

@ -0,0 +1,7 @@
/*Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all
customer and vice versa for those salesmen who belongs to a city and the customers who must have a grade.*/
SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL
AND b.grade IS NOT NULL;

@ -0,0 +1,9 @@
/*Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all
customer and vice versa for those salesmen who must belong a city which is not the same as his customer and the customers
should have an 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;

@ -0,0 +1,5 @@
/*Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all
customer and vice versa.*/
SELECT *
FROM salesman a
CROSS JOIN customer b;

@ -0,0 +1,6 @@
/*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.*/
SELECT *
FROM orders
NATURAL JOIN customer
NATURAL JOIN salesman;

@ -0,0 +1,13 @@
/*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
RIGHT OUTER JOIN orders c
ON c.customer_id=a.customer_id
WHERE c.purch_amt>=2000
AND a.grade IS NOT NULL;

@ -0,0 +1,10 @@
/*Write a SQL statement to make a list for the salesmen who works either for one or more customer or not yet join under any of
the customers who placed either one or more orders or no order to their 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
RIGHT OUTER JOIN orders c
ON c.customer_id=a.customer_id;

@ -0,0 +1,9 @@
/*Write a SQL statement to make a list in ascending order for the customer who holds a grade less than 300 and works either
through a salesman or by own.*/
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;

@ -0,0 +1,7 @@
/*Write a SQL statement to make a list in ascending order for the customer who works either through a salesman or by own.*/
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;

@ -0,0 +1,8 @@
/*Write a SQL statement to make a list in ascending order for the salesmen who works either for one or more customer or not
yet join under 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;

@ -0,0 +1,7 @@
/*Write a SQL statement to make a list with order no, purchase amount, customer name and their cities for those orders which
order amount between 500 and 2000.*/
SELECT a.ord_no,a.purch_amt,
b.cust_name,b.city
FROM orders a,customer b
WHERE a.customer_id=b.customer_id
AND a.purch_amt BETWEEN 500 AND 2000;

@ -0,0 +1,9 @@
/*Write a SQL statement to make a report with 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 is neither
in the list not have 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;

@ -0,0 +1,7 @@
/*Write a SQL statement to make a report with customer name, city, order no. order date, purchase amount for those customers
from the existing list who placed one or more orders or which order(s) have been placed by the customer who is not on the list*/
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;

@ -0,0 +1,9 @@
/* 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 find that either any of the existing customers have placed no order or placed one or more
orders.*/
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;

@ -0,0 +1,11 @@
/*Write a SQL statement to make a report with customer name, city, order number, order date, order amount salesman name and
commission to find that either any of the existing customers have placed no order or placed one or more orders by their salesman
or by own. */
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;

@ -0,0 +1,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 salesman.name AS "Salesman",
customer.cust_name, customer.city
FROM salesman,customer
WHERE salesman.city=customer.city;

@ -0,0 +1,4 @@
/*Write a SQL statement that finds out each order number followed by the name of the customers who made the order. */
SELECT orders.ord_no, customer.cust_name
FROM orders, customer
WHERE orders.customer_id = customer.customer_id;

@ -0,0 +1,8 @@
/*Write a SQL statement that produces all orders with the order number, customer name, commission rate and
earned commission amount for those customers who carry their grade more than 200 and served by an existing salesman.*/
SELECT ord_no, cust_name, commission AS "Commission%",
purch_amt*commission AS "Commission"
FROM salesman,orders,customer
WHERE orders.customer_id = customer.customer_id
AND orders.salesman_id = salesman.salesman_id
AND customer.grade>=200;

@ -0,0 +1,9 @@
/*Write a SQL statement that shorts out the customer and their grade who made an order. Each of the customers
must have a grade and served by at least a salesman, who belongs to a city.*/
SELECT customer.cust_name AS "Customer",
customer.grade AS "Grade"
FROM orders, salesman, customer
WHERE orders.customer_id = customer.customer_id
AND orders.salesman_id = salesman.salesman_id
AND salesman.city IS NOT NULL
AND customer.grade IS NOT NULL;

@ -0,0 +1,4 @@
/*Write a SQL statement to find the names of all customers along with the salesmen who works for them.*/
SELECT customer.cust_name, salesman.name
FROM customer,salesman
WHERE salesman.salesman_id = customer.salesman_id;

@ -0,0 +1,6 @@
/*Write a query to find those customers with their name and those salesmen with their name and city
who lives in the same city.*/
SELECT customer.cust_name,
salesman.name, salesman.city
FROM salesman, customer
WHERE salesman.city = customer.city;

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save