DBMS Exp-8
DBMS Exp-8
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);
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);