0% found this document useful (0 votes)
24 views10 pages

DBMS Project Queries

Uploaded by

vn2087
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)
24 views10 pages

DBMS Project Queries

Uploaded by

vn2087
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/ 10

ELECTRICITY BILLING AND CONSUMER

MANAGEMENT SYSTEM

 create database ebs and use database

1. create database ebs;

2. use ebs;

 Log in window

3. create table login(meter_no varchar(20), username varchar(30), name varchar(30), password


varchar(20), user varchar(20));

(INBUILT IN JAVA CODE)

4. insert into login (meter_no, username, name, password, user) values('"+smeter+"',


'"+susername+"', '"+sname+"', '"+spassword+"', '"+atype+"');

5. update login set username = '"+susername+"', password = '"+spassword+"', user = '"+atype+"'


where meter_no = '"+smeter+"'";

6. update login set username = '"+susername+"', password = '"+spassword+"', user = '"+atype+"'


where meter_no = '"+smeter+"'";

 Customer Window

7. create table customer(name varchar(20), meter_no varchar(20), address varchar(50), city


varchar(30), state varchar(30), email varchar(40), phone varchar(20));

8. select * from customer;

(INBUILT IN JAVA CODE)

9. insert into customer values('"+name+"', '"+meter+"', '"+address+"', '"+city+"', '"+state+"',


'"+email+"', '"+phone+"');
10.
insert into login values('"+meter+"', '', '"+name+"', '', '');

 Meter info

11. create table meter_info(meter_no varchar(20), meter_location varchar(20), meter_type


varchar(20), phase_code varchar(20), bill_type varchar(20), days varchar(20));

12. select * from meter_info;

(INBUILT IN JAVA CODE)


13. insert into meter_info values('"+meter+"', '"+location+"', '"+type+"', '"+code+"', '"+typebill+"',
'"+days+"')";
 Tax

14. create table tax(cost_per_unit varchar(20), meter_rent varchar(20), service_charge varchar(20),


service_tax varchar(20), swacch_bharat_cess varchar(20), fixed_tax varchar(20));

15. insert into tax values('9','47','22','57','6','18');

16. select*from tax;

 Bill

17. create table bill(meter_no varchar(20), month varchar(30), units varchar(20), totalbill
varchar(20), status varchar(20));

 Signup

18. select * from login where meter_no = '"+meter.getText()+"'")

19. update login set username = '"+susername+"', password = '"+spassword+"', user = '"+atype+"'
where meter_no = '"+smeter+"'";

 Deposite Details

20. select * from customer;

21. select * from bill;

22. select * from bill where meter_no = '"+meternumber.getSelectedItem()+"' and month =


'"+cmonth.getSelectedItem()+"'";

 Bill Details

23. select * from bill where meter_no = '"+meter+"'";

 Calculate bill

24. select * from customer where meter_no = '"+meternumber.getSelectedItem()+"'");

25. select * from customer where meter_no = '"+meternumber.getSelectedItem()+"'" ;

26. insert into bill values('"+meter+"', '"+month+"', '"+units+"', '"+totalbill+"', 'Not Paid');

 PayBill

27. select * from customer where meter_no = '"+meter+"'"

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)

31. use ebs;


32. show tables;
33. select*from customer;
34. select*from meter_info;
35. select*from login;
36. select*from tax;
37. select*from bill;

38. Retrieve the names of customers who have unpaid bills: -

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:

SELECT c.name, m.meter_location


FROM customer c
INNER JOIN meter_info m ON c.meter_no = m.meter_no;

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:

SELECT c.name, m.meter_location, b.status


FROM customer c
INNER JOIN meter_info m ON c.meter_no = m.meter_no
INNER JOIN bill b ON c.meter_no = b.meter_no
WHERE b.month = 'March';

60. Find the names of customers along with their corresponding states and tax amounts:

SELECT c.name, c.state, t.cost_per_unit


FROM customer c
INNER JOIN tax t ON c.state = t.state;

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:

CREATE TRIGGER update_tax_on_customer_add


AFTER INSERT ON customer
FOR EACH ROW
BEGIN
INSERT INTO tax (state, cost_per_unit)
VALUES (NEW.state, '10');
END;

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)));

83. Insert a new customer into the database:


INSERT INTO customer (name, meter_no, address, city, state, email, phone) VALUES ('John Doe',
'123456', '123 Main St', 'Anytown', 'CA', '[email protected]', '555-1234');

84. Insert a new meter information:


INSERT INTO meter_info (meter_no, meter_location, meter_type, phase_code, bill_type, days)
VALUES ('123456', 'Apartment A', 'Analog', 'Single', 'Monthly', '30');

85. Insert a new tax information:


INSERT INTO tax (cost_per_unit, meter_rent, service_charge, service_tax, swacch_bharat_cess,
fixed_tax) VALUES ('10', '50', '20', '40', '5', '15');

86. Insert a new bill record:


INSERT INTO login (meter_no, username, name, password, user) VALUES ('123456', 'john123', 'John
Doe', 'password123', 'customer');

87. Insert a new login record:


INSERT INTO login (meter_no, username, name, password, user) VALUES ('123456', 'john123', 'John
Doe', 'password123', 'customer');

88. Insert multiple customers at once:


INSERT INTO customer (name, meter_no, address, city, state, email, phone) VALUES
('Alice Smith', '789012', '456 Oak St', 'Otherville', 'NY', '[email protected]', '555-5678'),
('Bob Johnson', '345678', '789 Elm St', 'Another City', 'TX', '[email protected]', '555-6789'),
('Emily Brown', '901234', '101 Pine St', 'Somewhere', 'FL', '[email protected]', '555-7890');
89. Insert a new meter location:
INSERT INTO meter_info (meter_no, meter_location, meter_type, phase_code, bill_type, days)
VALUES ('789012', 'Apartment B', 'Digital', 'Single', 'Bi-monthly', '60');

90. Insert a new tax record:


INSERT INTO tax (cost_per_unit, meter_rent, service_charge, service_tax, swacch_bharat_cess,
fixed_tax) VALUES ('12', '55', '22', '45', '6', '20');

91. Insert a new bill record:


INSERT INTO bill (meter_no, month, units, totalbill, status) VALUES ('789012', 'February', '120', '200',
'Not Paid');

92. Insert a new login record:


INSERT INTO login (meter_no, username, name, password, user) VALUES ('789012', 'alice456', 'Alice
Smith', 'securepass', 'customer');

93. Update customer's email address:

UPDATE customer SET email = '[email protected]' WHERE meter_no = '123456';

94. Update meter location for a specific meter:

UPDATE meter_info SET meter_location = 'Apartment C' WHERE meter_no = '789012';

95. Update tax information for a specific state:


UPDATE tax SET cost_per_unit = '11', meter_rent = '60' WHERE state = 'CA';

96. Update bill status to 'Paid' for a specific meter and month:
UPDATE bill SET status = 'Paid' WHERE meter_no = '123456' AND month = 'January';

97. Update login password for a specific user:


UPDATE login SET password = 'new_password' WHERE username = 'john123';

98. Update customer's phone number:


UPDATE customer SET phone = '555-4321' WHERE meter_no = '123456';

99. Update meter type for a specific meter


UPDATE meter_info SET meter_type = 'Smart' WHERE meter_no = '789012';

100. Update tax information for a specific state:


UPDATE tax SET service_charge = '25', service_tax = '50' WHERE state = 'NY';

You might also like