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

DBMS Exp-8

The document outlines SQL queries for an employee database, detailing how to retrieve various information such as employees working for a specific company, customers who have placed orders, and those who have not. It includes examples of SQL expressions for finding customer details based on order amounts and spending. Additionally, it addresses how to calculate average spending and identify customers with the most orders or highest total spending.

Uploaded by

vansh sharma
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)
10 views5 pages

DBMS Exp-8

The document outlines SQL queries for an employee database, detailing how to retrieve various information such as employees working for a specific company, customers who have placed orders, and those who have not. It includes examples of SQL expressions for finding customer details based on order amounts and spending. Additionally, it addresses how to calculate average spending and identify customers with the most orders or highest total spending.

Uploaded by

vansh sharma
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

VINAYAK GARG

2200290110196
CSIT -5C2
Experiment -8 :-
employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Consider the above employee database where the primary keys are
underlined.
Give an expression in SQL for each of the following queries.

a. Find the names of all employees who work for First Bank
Corporation.
SELECT first_name, last_name
FROM Customers
WHERE country = 'USA';
b. Find the names and ages of all customers who have placed an
order.
SELECT DISTINCT c.first_name, c.last_name, c.age
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id;

c. Find the names, countries, and ages of customers who have placed an
order for an amount greater than 500.
sql
Copy code
SELECT DISTINCT c.first_name, c.last_name, c.country, c.age
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
WHERE o.amount > 500;

d. Find all customers in the database who have not placed an order.
SELECT c.first_name, c.last_name
FROM Customers c
WHERE c.customer_id NOT IN (
SELECT customer_id
FROM Orders );

e. Find all customers who have spent more than the average order amount.
WITH avg_order AS (
SELECT AVG(amount) AS avg_amount
FROM Orders
)
SELECT DISTINCT c.first_name, c.last_name
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
WHERE o.amount > (SELECT avg_amount FROM avg_order);

f. Find the customer who has placed the most orders.


SELECT c.first_name, c.last_name, COUNT(o.order_id) AS total_orders
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_orders DESC
LIMIT 1;

g. Find the customer with the highest total spending on orders.


SELECT c.first_name, c.last_name, SUM(o.amount) AS total_spent
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_spent DESC
LIMIT 1;

h. Find the countries where customers, on average, spend more than the
average spending in the UAE.
WITH avg_uae_spending AS (
SELECT AVG(o.amount) AS avg_uae_amount
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
WHERE c.country = 'UAE'
)
SELECT c.country, AVG(o.amount) AS avg_country_spending
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.country
HAVING AVG(o.amount) > (SELECT avg_uae_amount FROM
avg_uae_spending);

You might also like