DBMS Project Queries
DBMS Project Queries
MANAGEMENT SYSTEM
2. use ebs;
Log in window
Customer Window
Meter info
Bill
17. create table bill(meter_no varchar(20), month varchar(30), units varchar(20), totalbill
varchar(20), status varchar(20));
Signup
19. update login set username = '"+susername+"', password = '"+spassword+"', user = '"+atype+"'
where meter_no = '"+smeter+"'";
Deposite Details
Bill Details
Calculate bill
26. insert into bill values('"+meter+"', '"+month+"', '"+units+"', '"+totalbill+"', 'Not Paid');
PayBill
28. select * from bill where meter_no = '"+meter+"' AND month = 'January';
29. select * from bill where meter_no = '"+meter+"' AND month = '"+cmonth.getSelectedItem()+"'");
30. update bill set status = 'Paid' where meter_no = '"+meter+"' AND
month='"+cmonth.getSelectedItem()+"'");
Extra ( code I can run without affecting the code , sperately on my sql directly)
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE status = 'Not Paid');
39. Find the total number of units consumed by customers living in a particular city:
SELECT SUM(units)
FROM bill
WHERE meter_no IN (SELECT meter_no FROM customer WHERE city = 'YourCityName');
40. Find the total number of customers who have bills greater than the average bill amount:-
SELECT COUNT(*)
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE totalbill > (SELECT AVG(totalbill) FROM
bill));
41. Retrieve the names of customers who are also registered users:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '');
42. Retrieve the names of customers along with their corresponding meter locations:
43. Create a trigger to update the bill status to 'Paid' when a payment is made:
CREATE TRIGGER update_bill_status
AFTER INSERT ON payment
FOR EACH ROW
BEGIN
UPDATE bill
SET status = 'Paid'
WHERE meter_no = NEW.meter_no AND month = NEW.month;
END;
44. Retrieve the total number of units consumed by customers who live in states with more than
100 customers:
SELECT SUM(units)
FROM bill
WHERE meter_no IN (SELECT meter_no FROM customer WHERE state IN (SELECT state FROM (SELECT
state, COUNT(*) AS num_customers FROM customer GROUP BY state) AS
states_with_more_than_100 WHERE num_customers > 100));
45. Find the names of customers who have bills exceeding the average bill amount for the month of
January:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE month = 'January' AND totalbill > (SELECT
AVG(totalbill) FROM bill WHERE month = 'January'));
46. Retrieve the names of customers who live in cities where the average bill amount exceeds 50
units:
SELECT name
FROM customer
WHERE city IN (SELECT city FROM (SELECT city, AVG(units) AS avg_units FROM bill GROUP BY city) AS
city_avg WHERE avg_units > 50);
47. Find the total number of customers who live in states with cities having an average bill amount
greater than 60 units:
SELECT COUNT(*)
FROM customer
WHERE state IN (SELECT state FROM (SELECT state, AVG(units) AS avg_units FROM bill GROUP BY
state) AS state_avg WHERE avg_units > 60);
48. Retrieve the names of customers who are both registered users and have unpaid bills:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no IN (SELECT
meter_no FROM bill WHERE status = 'Not Paid');
49. Find the names of customers who are either registered users or have bills exceeding 100 units:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') OR meter_no IN (SELECT
meter_no FROM bill WHERE units > 100);
50. Retrieve the names of customers along with their corresponding meter types:
SELECT c.name, m.meter_type
FROM customer c
INNER JOIN meter_info m ON c.meter_no = m.meter_no;
51. Find the names of customers along with their corresponding bill status for the month of
February:
SELECT c.name, b.status
FROM customer c
INNER JOIN bill b ON c.meter_no = b.meter_no
WHERE b.month = 'February';
52. Create a trigger to update the tax amount whenever a new bill is generated:
CREATE TRIGGER update_tax_amount
AFTER INSERT ON bill
FOR EACH ROW
BEGIN
UPDATE bill
SET totalbill = totalbill + (SELECT cost_per_unit FROM tax) * NEW.units;
END;
53. Create a trigger to update the meter type whenever a meter location is changed:
CREATE TRIGGER update_meter_type
AFTER UPDATE ON meter_info
FOR EACH ROW
BEGIN
UPDATE customer
SET meter_type = NEW.meter_type
WHERE meter_no = NEW.meter_no;
END;
54. Retrieve the names of customers who have bills greater than the average bill amount for the
year:
SELECT SUM(units)
FROM bill
WHERE meter_no IN (SELECT meter_no FROM customer WHERE city IN (SELECT city FROM (SELECT
city, COUNT(*) AS num_users FROM login WHERE user <> '' GROUP BY city) AS
cities_with_more_than_two_users WHERE num_users > 2));
55. Retrieve the names of customers who live in states where the maximum bill amount for any
month exceeds 200 units:
SELECT name
FROM customer
WHERE state IN (SELECT state FROM (SELECT state, MAX(totalbill) AS max_bill FROM bill GROUP BY
state) AS states_with_max_bill WHERE max_bill > 200);
56. Find the total number of customers who live in cities where the minimum bill amount for any
month is less than 50 units:
SELECT COUNT(*)
FROM customer
WHERE city IN (SELECT city FROM (SELECT city, MIN(totalbill) AS min_bill FROM bill GROUP BY city) AS
cities_with_min_bill WHERE min_bill < 50);
57. Retrieve the names of customers who are registered users but have not received any bills yet:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no NOT IN (SELECT
meter_no FROM bill);
58. Find the names of customers who are neither registered users nor have any bills:
SELECT name
FROM customer
WHERE meter_no NOT IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no NOT IN
(SELECT meter_no FROM bill);
59. Retrieve the names of customers along with their corresponding meter locations and bill
statuses for the month of March:
60. Find the names of customers along with their corresponding states and tax amounts:
60.Create a trigger to update the bill status to 'Paid' when the totalbill exceeds 1000 units:
CREATE TRIGGER update_bill_status_on_threshold
AFTER INSERT ON bill
FOR EACH ROW
BEGIN
IF NEW.totalbill > 1000 THEN
UPDATE bill
SET status = 'Paid'
WHERE meter_no = NEW.meter_no AND month = NEW.month;
END IF;
END;
61. create a trigger to update the tax amount whenever a new customer is added:
62. Retrieve the names of customers who have bills exceeding the average bill amount for the
current month:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE month = 'CurrentMonth' AND totalbill >
(SELECT AVG(totalbill) FROM bill WHERE month = 'CurrentMonth'));
63. Find the total number of units consumed by customers living in states where the average bill
amount is greater than 75 units:
SELECT SUM(units)
FROM bill
WHERE meter_no IN (SELECT meter_no FROM customer WHERE state IN (SELECT state FROM (SELECT
state, AVG(totalbill) AS avg_bill FROM bill GROUP BY state) AS states_with_avg_bill WHERE avg_bill >
75));
64. Retrieve the names of customers who have bills greater than the average bill amount for the
month of February and live in cities where the average bill amount exceeds 50 units:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE month = 'February' AND totalbill > (SELECT
AVG(totalbill) FROM bill WHERE month = 'February') AND city IN (SELECT city FROM (SELECT city,
AVG(totalbill) AS avg_bill FROM bill GROUP BY city) AS cities_with_avg_bill WHERE avg_bill > 50));
65. Find the total number of customers who live in states where the maximum number of units
consumed is greater than 500:
SELECT COUNT(*)
FROM customer
WHERE state IN (SELECT state FROM (SELECT state, MAX(units) AS max_units FROM bill GROUP BY
state) AS states_with_max_units WHERE max_units > 500);
66. Retrieve the names of customers who are registered users and have received bills for all months:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no IN (SELECT
meter_no FROM bill GROUP BY meter_no HAVING COUNT(DISTINCT month) = 12);
67. Find the names of customers who are registered users but have not received bills for the last
three months:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no NOT IN (SELECT
meter_no FROM bill WHERE month IN ('LastMonth', 'SecondLastMonth', 'ThirdLastMonth'));
68. Retrieve the names of customers along with their corresponding meter types and bill statuses
for the month of April:
SELECT c.name, m.meter_type, b.status
FROM customer c
INNER JOIN meter_info m ON c.meter_no = m.meter_no
LEFT JOIN bill b ON c.meter_no = b.meter_no AND b.month = 'April';
69. Find the names of customers along with their corresponding states and service charges:
SELECT c.name, c.state, t.service_charge
FROM customer c
INNER JOIN tax t ON c.state = t.state;
70. Create a trigger to update the bill status to 'Unpaid' when a bill is deleted:
CREATE TRIGGER update_bill_status_on_delete
BEFORE DELETE ON bill
FOR EACH ROW
BEGIN
UPDATE bill
SET status = 'Unpaid'
WHERE meter_no = OLD.meter_no AND month = OLD.month;
END;
71. Create a trigger to update the tax amount whenever a new meter type is added:
CREATE TRIGGER update_tax_on_meter_type_add
AFTER INSERT ON meter_info
FOR EACH ROW
BEGIN
UPDATE tax
SET cost_per_unit = cost_per_unit + 1
WHERE meter_type = NEW.meter_type;
END;
72. Retrieve the names of customers who have bills greater than the average bill amount for the
previous month:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE month = 'PreviousMonth' AND totalbill >
(SELECT AVG(totalbill) FROM bill WHERE month = 'PreviousMonth'));
73. Find the total number of units consumed by customers living in cities where the minimum bill
amount is less than 50 units:
SELECT SUM(units)
FROM bill
WHERE meter_no IN (SELECT meter_no FROM customer WHERE city IN (SELECT city FROM (SELECT
city, MIN(totalbill) AS min_bill FROM bill GROUP BY city) AS cities_with_min_bill WHERE min_bill <
50));
74. Retrieve the names of customers who have bills greater than the average bill amount for the
month of May and live in states where the average bill amount exceeds 60 units:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE month = 'May' AND totalbill > (SELECT
AVG(totalbill) FROM bill WHERE month = 'May') AND state IN (SELECT state FROM (SELECT state,
AVG(totalbill) AS avg_bill FROM bill GROUP BY state) AS states_with_avg_bill WHERE avg_bill > 60));
75. Find the total number of customers who live in states where the maximum number of units
consumed is greater than 700:
SELECT COUNT(*)
FROM customer
WHERE state IN (SELECT state FROM (SELECT state, MAX(units) AS max_units FROM bill GROUP BY
state) AS states_with_max_units WHERE max_units > 700);
76. Retrieve the names of customers who are registered users and have received bills for at least
one month:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no IN (SELECT
meter_no FROM bill GROUP BY meter_no HAVING COUNT(DISTINCT month) >= 1);
77. Find the names of customers who are registered users but have not received bills for the last
two months:
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM login WHERE user <> '') AND meter_no NOT IN (SELECT
meter_no FROM bill WHERE month IN ('LastMonth', 'SecondLastMonth'));
78. Retrieve the names of customers along with their corresponding meter types and bill statuses
for the month of June:
SELECT c.name, m.meter_type, b.status
FROM customer c
INNER JOIN meter_info m ON c.meter_no = m.meter_no
LEFT JOIN bill b ON c.meter_no = b.meter_no AND b.month = 'June';
79. Find the names of customers along with their corresponding states and meter types:
SELECT c.name, c.state, m.meter_type
FROM customer c
INNER JOIN meter_info m ON c.meter_no = m.meter_no;
80. Create a trigger to update the bill status to 'Unpaid' when a bill is updated to have zero units:
CREATE TRIGGER update_bill_status_on_zero_units
BEFORE UPDATE ON bill
FOR EACH ROW
BEGIN
IF NEW.units = 0 THEN
UPDATE bill
SET status = 'Unpaid'
WHERE meter_no = NEW.meter_no AND month = NEW.month;
END IF;
END;
81. Create a trigger to update the tax amount whenever a new state is added:
CREATE TRIGGER update_tax_on_state_add
AFTER INSERT ON customer
FOR EACH ROW
BEGIN
INSERT INTO tax (state, cost_per_unit)
VALUES (NEW.state, '12');
END;
82. Retrieve the names of customers who have bills greater than the average bill amount for the
current year
SELECT name
FROM customer
WHERE meter_no IN (SELECT meter_no FROM bill WHERE YEAR(date) = YEAR(CURRENT_DATE) AND
totalbill > (SELECT AVG(totalbill) FROM bill WHERE YEAR(date) = YEAR(CURRENT_DATE)));
96. Update bill status to 'Paid' for a specific meter and month:
UPDATE bill SET status = 'Paid' WHERE meter_no = '123456' AND month = 'January';