Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

dbms_file

Uploaded by

hjyt987654
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

dbms_file

Uploaded by

hjyt987654
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PART-A

Q.1 Creation of a table.

Create table teacher(Tno integer primary key,Name varchar(30) NOT


NULL,age integer,Dept varchar(20),Date_of_join varchar(20),salary
integer,bonus integer,sex varchar(1));

OUTPUT:-

Q 2. Insert 5 records in this teacher table.

Insert into teacher values(1,’himanshu’,18,’computer’,’2005-12-


12’,200000,2000,’M’),(2,’mohan’,20,’science’,’2018-08-
12’,20000,1000,’M’),(3,’sakshi’,18,’computer’,’2018-01-
10’,50000,5000,’F’),(4,’priya’,25,’commerce’,’2020-05-
01’,30000,1000,’F’),(5,’rajesh’,21,’science’,’2024-09-
15’,200000,2000,’M’);

OUTPUT:-

1
Q 3. List the names in capital of the male teachers who are in
Science or commerce.
Department in the descending order of their salary.

Select upper(Name) as Name,Dept,salary from teacher where sex = ‘m’


and dept IN (‘science’,commerce’) 0rder by salary desc;

OUTPUT:-

Q 4. Calculate the total salary given department wise.

Select Dept,sum(salary) as Total_salary from teacher group by Dept;

OUTPUT:-

2
Q 5. Increase the salary of all those teachers who have joined
before 10-jan-2018 by 20%.

Update teacher set salary = salary * 1.20 where Date_of_join < ‘2018-01-
12’;

OUTPUT:-

Q 6. Display the average salary given for computer dept.

Select avg(salary) from teacher where Dept = ‘computer’;

OUTPUT:-

Q 7. Display the department names where the total salary


disbursed is more than 500000.

Select Dept from teacher group by Dept having sum(salary) > 500000.

Q 8. Display the teacher name, age and salary rounded off to


hundredth place.

Select Name,age, Round(salary,2) as rounded_salary from teacher;

3
OUTPUT:-

Q 9. Display the teacher name along with the length of each


name.

Select Name,Position(‘a’ in name) as Position_of_a_in_Name from


teacher;

OUTPUT:-

Q 10. Display those department names where the number of


teacher’s working is more than 5.

Q 11. Display the position of the occurrence of ’a’ in all the


teacher names.

Select Name,position(“a” in Name) as Matchposition from teacher;

4
OUTPUT:-

Q 12. Display the highest and lowest salary value for each
department.

Select Dept,Max(salary) as Max_Salary,min(salary) as Min_salary from


teacher group by Dept;

OUTPUT:-

Q 13. Display the total number of records in the table.

Select count(*) as Total_Records from teacher;

OUTPUT:-

5
Q 14. Display those department names where the maximum
bonus given is more than 20000.

Select Dept,max(bonus) as Max_bonus_greater_than_2000 from teacher


group by Dept having max(bonus) > 2000;

OUTPUT:-

Q 15. Display a report showing the average bonus given to


every department.

Select Dept,avg(bonus) as Average_Bonus from teacher group by Dept;

OUTPUT:-

Q 16. Display the count of teachers in every department.

Select Dept,count(Name) as Faculty_in_each_Dept from teacher group


by Dept;

OUTPUT:-

6
Q 17. Display the name of teachers who have joined in the
month of January, April and July.

Select Name from teacher where month(Date_of_join) IN(‘1’,’5’,’7’);

OUTPUT:-

Q 18. Display the count of male and female teachers in the


school.

Select Sex,count(*) as Male_Female_count from teacher group by sex;

OUTPUT:-

PART-B

Q19.To display minimum amount transaction from the table.

Select min(Amount) as Min_Amount_Transact from teacher;

7
OUTPUT:-

Q 20.To display total amount withdrawn from table.

Select sum(Amount) as Total_Amount_Transact from Transact;

OUTPUT:-

Q 21. To display number of deposited transaction.

Select count(*) from Transact where Type = ‘Deposit’;

OUTPUT:-

Q 22. To display ANO, DOT, AMOUNT for maximum amount


transaction.

Select ANO,DOT,Amount from Transact where Amount= (select


max(Amount) from Transact);

8
OUTPUT:-

Q23 . SELECT ANO, COUNT(*), MIN(AMOUNT) FROM TRANSACT


GROUP BY ANO HAVING COUNT (*)> 1.

Select ANO,Count(*) as Transaction_count,min(Amount) as


Minimum_Amount from Transact group by ANO having count(*) > 1;

OUTPUT:-

Q24 . SELECT COUNT(*), SUM(AMOUNT) FROM TRANSACT


WHERE DOT <= '2017-06-01';

SELECT COUNT(*) as Transaction, SUM(AMOUNT) as Total_Amount


FROM Transact WHERE DOT <= '2017-06-01';

OUTPUT:-

9
PART-C

Q 25. Create the given tables

Create table customer(ID int,Name varchar(10),Age int,ADDRESS


varchar(20),Salary int);

Create table orders(OID int,DATE DATE,Customer_ID int,Amount int);

OUTPUT:-

Q26. Insert records.


A)Table of Customer
Insert into customer
values(1,’Ramesh’,32,’Ahmedabad’,2000),(2,’Vivek’,25,’Delhi’,1500),(3,’K
aushik’,25,’Mumbai’,6500),(4,
‘Hardik’27,’Bhopal’,8500),(5,’Komal’,22,’MP’,4500),(6,’Mukta’,24,’Indore’,
10000),(7,’Chitra’,23,’Kota’,2000);

OUTPUT:-

10
B) Table of orders

Insert into orders values(102,’2009-10-08’,3,3000),(100,’2009-10-


08’,3,1500),(101,’2009-11-20’,2,1560),(103,’2008-05-20’,4,2000);

OUTPUT:-

Q 27 . Create primary key, and foreign key.

Alter table add primary key(ID);

OUTPUT:-

Alter table orders add foreign key(Customer_ID) references customer(ID);

OUTPUT:-

11
Q 29. join these two tables using ‘FULL JOIN’.

Select * from customer left orders on customer.ID = orders.Customer_ID


UNION ALL select * from orders right join customer on customer.ID =
orders.Customer_ID;

OUTPUT:-

Q30. join these two tables using ‘LEFT OUTER JOIN’.

Select * from customer left join orders on customer.ID =


orders.Customer_ID;

OUTPUT:-

12
Q31 . join these two tables using ‘RIGHT OUTER JOIN’.

Select * from customer right join orders on customer.ID =


orders.Customer_ID;

OUTPUT:-

Q32. write a query if you want to know the total amount of


salary on each customer.

Select Name,sum(Salary) as Total_Salary from customer group by Name;

OUTPUT:-

13
Q33 . MYSQL C PROGRAM SQL VERSION.

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)


{
printf("MySQL client version: %s\n", mysql_get_client_info());

exit(0);

OUTPUT:-

Q34. MYSQL C PROGRAM CREATE DATABASE.


#include<mysql.h>
#include<stdio.h>

14
#include<stdlib.h>

int main(int argc, char** argv)


{
MYSQL* con = mysql_init(NULL);

if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}

if (mysql_real_connect(con, "localhost", "root", "Him2005@",


NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

if (mysql_query(con, "CREATE DATABASE nayadatabase"))


{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

mysql_close(con);
exit(0);

OUTPUT:-

15
Q35. MYSQL C PROGRAM CREATE AND POPULATE TABLE.
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

void finish_with_error(MYSQL* con)


{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char** argv)


{
MYSQL* con = mysql_init(NULL);

if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}

if (mysql_real_connect(con, "localhost", "root", "Him2005@",


"nayadatabase", 0, NULL, 0) == NULL)
{
finish_with_error(con);

16
}

if (mysql_query(con, "DROP TABLE IF EXISTS cars")) {


finish_with_error(con);
}

if (mysql_query(con, "CREATE TABLE cars(id INT PRIMARY KEY


AUTO_INCREMENT, name VARCHAR(255), price INT)")) {
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars VALUES(1,'Audi',52642)")) {


finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars VALUES(2,'Mercedes',57127)"))


{
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars VALUES(3,'Skoda',9000)")) {


finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars VALUES(4,'Volvo',29000)")) {


finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars VALUES(5,'Bentley',350000)"))


{
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars VALUES(6,'Citroen',21000)")) {


finish_with_error(con);
}

17
if (mysql_query(con, "INSERT INTO cars VALUES(7,'Hummer',41400)"))
{
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO cars


VALUES(8,'Volkswagen',21600)")) {
finish_with_error(con);
}

mysql_close(con);
exit(0);
}

OUTPUT:-

Q36. MYSQL C PROGRAM RETRIEVE DATA.

#include <mysql.h>
#include <stdio.h>

18
#include <stdlib.h>

void finish_with_error(MYSQL* con)


{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char** argv)


{
MYSQL* con = mysql_init(NULL);

if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}

if (mysql_real_connect(con, "localhost", "root", "Him2005@",


"nayadatabase", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}

if (mysql_query(con, "SELECT * FROM cars"))


{
finish_with_error(con);
}

MYSQL_RES* result = mysql_store_result(con);

if (result == NULL)
{
finish_with_error(con);
}

19
int num_fields = mysql_num_fields(result);

MYSQL_ROW row;

while ((row = mysql_fetch_row(result)))


{
for (int i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}

printf("\n");
}

mysql_free_result(result);
mysql_close(con);

exit(0);
}

OUTPUT:-

Q37. MYSQL C PROGRAM LAST INSERTED ROW ID.

20
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

void finish_with_error(MYSQL *con)


{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char **argv)


{
MYSQL *con = mysql_init(NULL);

if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}

if (mysql_real_connect(con, "localhost", "root", "Him2005@",


"test2db", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}

if (mysql_query(con, "DROP TABLE IF EXISTS writers"))


{
finish_with_error(con);
}

char *sql = "CREATE TABLE writers(id INT PRIMARY KEY


AUTO_INCREMENT, name VARCHAR(255))";

if (mysql_query(con, sql))
{

21
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO writers(name) VALUES('Leo


Tolstoy')"))
{
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO writers(name) VALUES('Jack


London')"))
{
finish_with_error(con);
}

if (mysql_query(con, "INSERT INTO writers(name) VALUES('Honore de


Balzac')"))
{
finish_with_error(con);
}

int id = mysql_insert_id(con);

printf("The last inserted row id is: %d\n", id);

mysql_close(con);
exit(0);
}

OUTPUT:-

22
Q38. MYSQL C PROGRAM COLUMNS HEADERS

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

void finish_with_error(MYSQL* con)


{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char** argv)


{
MYSQL* con = mysql_init(NULL);

if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}

if (mysql_real_connect(con, "localhost", "root", "Him2005@",


"nayadatabase", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}

if (mysql_query(con, "SELECT * FROM cars LIMIT 3"))


{
finish_with_error(con);
}

MYSQL_RES* result = mysql_store_result(con);

if (result == NULL)

23
{
finish_with_error(con);
}

int num_fields = mysql_num_fields(result);

MYSQL_ROW row;
MYSQL_FIELD* field;

while ((row = mysql_fetch_row(result)))


{
for (int i = 0; i < num_fields; i++)
{
if (i == 0)
{
while (field = mysql_fetch_field(result))
{
printf("%s ", field->name);
}

printf("\n");
}

printf("%s ", row[i] ? row[i] : "NULL");


}
}

printf("\n");

mysql_free_result(result);
mysql_close(con);

exit(0);
}

OUTPUT:-

24
25

You might also like