Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SQL EXERCISE 1 : To generate electricity bill for five consumers.

1. Create a table with the following columns :

Field name Data type Description


RR_NO Varchar(2) Electric meter number
CUS_NAME Varchar(25) Consumer name
BILL_DATE Date Billing date
UNITS Number(4) Units consumed
2. Describe the structure of the table.
3. Insert five records into the table.
4. Add new columns to the table :
BILL_AMT int(10)
DUE_DATE date
5. Compute bill amount as per the following rules:
Minimum amount = Rs. 50
First 100 units = Rs 4.50 / unit
If units > 100 = Rs. 5.50 / unit

6. Compute the due date as billing date + 15 days


7. List all the bills generated.

Solution :

1. MySQL> CREATE TABLE ebill (rr_no varchar(2) , cus_name varchar(25) ,


bill_date date , units number(4));
2. MySQL> DESCRIBE ebill;
3. MySQL> INSERT INTO ebill VALUES(‘101’ , ‘AJAY’ , ’10-MAY-2020’);
Repeat the INSERT command for 5 records with different values
4. MySQL> ALTER TABLE ebill ADD (BILL_AMT number(6,2) , DUE_DATE
date);
5. MySQL> UPDATE ebill SET BILL_AMT = 50 + units*4.50 WHERE units
<= 100;
MySQL> UPDATE ebill SET BILL_AMT = 50 + 100*4.50 +
(units-100)*5.50 WHERE units > 100;
6. MySQL> UPDATE ebill SET DUE_DATE = BILL_DATE + 15;
7. MySQL> SELECT * FROM ebill;
—------------------------- X —-----------------------
SQL EXERCISE 2 : To create a student database.
1. Create a table with the following columns :
Field name Data type Description
REGNO int(4) Student register number
NAME char(25) Student name
COMP_MARK int(3) Marks obtained in Comp Sc
ECO_MARK int(3) Marks obtained in Economics
BS_MARK int(3) Marks obtained in Business St
ACC_MARK int(3) Marks obtained in Accountancy
2. Add 5 records to the table.
3. Alter the table by adding TOTAL , PERCENTAGE and RESULT columns.
4. Compute TOTAL and PERCENTAGE.
5. Compute RESULT as PASS if the marks obtained in each subject is >=35.
6. Compute RESULT as FAIL if the marks obtained in any subject is < 35.
7. List all the student records who have “PASSed”.
8. Count the number of students who have “FAILed”.
9. Sort the table according to the order of REGNO.
Solution :
1. MySQL> CREATE TABLE student (REGNO int(4) , NAME char(25) ,
COMP_MARK int(3) , ECO_MARK int(3) , BS_MARK int(3) , ACC_MARK
int(3));
2. MySQL> INSERT INTO student VALUES (104 , ‘AMAR’ , 45 , 67 , 23 , 67);
Repeat the INSERT command for 5 records with different values
3. MySQL> ALTER TABLE student ADD (TOTAL int(3) , PERCENTAGE
number(5,2) , RESULT char(4));
4. MySQL> UPDATE student SET TOTAL = COMP_MARK + ECO_MARK +
BS_MARK + ACC_MARK;
MySQL> UPDATE student SET PERCENTAGE = TOTAL / 4.0;
5. MySQL> UPDATE student SET RESULT = ‘PASS’ WHERE (COMP_MARK
>= 35 AND ECO_MARK >= 35 AND BS_MARK >= 35 AND ACC_MARK
>= 35);
6. MySQL> UPDATE student SET RESULT = ‘PASS’ WHERE (COMP_MARK
< 35 OR ECO_MARK < 35 OR BS_MARK < 35 OR ACC_MARK < 35);
7. MySQL> SELECT * FROM student WHERE RESULT = ‘PASS’;
8. MySQL> SELECT COUNT(*) FROM student WHERE RESULT = ‘FAIL’;
9. MySQL> SELECT * FROM student ORDER BY REGNO;
—------------------------- X —-------------------------

You might also like