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

SQL

SQL Queries for computer science class 12 cbse

Uploaded by

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

SQL

SQL Queries for computer science class 12 cbse

Uploaded by

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

SQL:

1.Given the following tables for a database LIBRARY:

Table: BOOKS

Table: ISSUED

a)To show book name, author name and price of books of First Publ. Publisher:

Query: Select book_name, author_name, price from books where publisher=’First Publ’;

Output:

b) To list the names from books of fiction type:

Query: select Book_name from books where type=’Fiction’;


Output:

c) To display the names and price from books in descending order of their price.

Query: select name, price from books order by price desc;

Output:

d) To Increase quantity by 2 of TDH publisher.

Query: Update books set qty=qty+2 where publishers='TDH';

Output:

e) To display the book_id, book name and QTY_issued for all books which have been issued.

Query: select book_ID, book_name, QTY_issued from books natural join issued;
Output:

f) To insert a new row in the table Issued having the following data: "F0003", 1.

Query: insert into issued values(‘F0003’,1);

Output:

g) Show the unique publisher name from books table of price more than 400.

Query: Select distinct(publisher) from books where price>400;

Output:

h) Display the maximum price of books whose quantity is greater than 15.

Query: select max(price) from books where qty>15;


Output:

2. Consider the following dept and worker tables.

Table: DEPT Table: Worker

a) To display Wno, Name, Gender from the table WORKER in descending order of Wno

Query: select wno, name, gender from worker order by wno desc;

Output:

b)To display the Name of all FEMALE workers from the table WORKER

Query: select name from worker where gender=’Female’;

Output:
c) To display the Wno and Name of those workers from the table WORKER who are born betweeb '1987-
01-01 and 1991-12-01.

Query: select wno,name from worker where dob between ‘1987-01-01’ and ‘1991-12-01’;

Output:

e)To display unique department from dept table.

Query: select distinct(department) from dept;

Output:

f)To display name, department,city for all workers


Query: select name,department,city from dept natural join worker;

Output:

g)To display name, department city for all workers of worker number less than 1003.

Query: select name,department,city from dept natural join worker where wno<1003;

Output:

h)To display name of the department whose name starts with M.

Query: select department from dept where department like ‘m%’;

Output:

i)To display name of the department whose name ends with E


Query: select department from dept where department like ‘%e’;

Output:

j) To count number of female workers.

Query: Select count(gender) from worker where gender=’female’;

Output:

3. Assuming the ‘Sales’ table has the following structure:


1)Retrieve all sales amount

Query: select sales_amount from sales;

Output:

2)Retrieve total sales amount

Query: select sum(sales_amount) from sales;

Output:

3)Find highest sales amount

Query: select max(sales_amount) from sales;

Output:

4)Retrieve unique product categories


Query: select distinct(Category) from sales;

Output:

5)Count the number of sales

Query: select count(sales_amount) from sales;

Output:

6)retrieve sales above $400

Query: select sales_amount from sales where sales_amount>400;

Output:

7)retrieve sales with NULL category


Query: select sales_amount from sales where category=null;

Output:

8) Retrieve all the records from sales of category electronics

Query: select sales_amount from sales where category=’electronics’;

Output:

9)Order sales by amount(descending)

Query: select sales_amount from sales order by sales_amount desc;

Output:

10) calculate average sales amount

Query: select avg(sales_amount) from sales;

Output:

11)Group Sales by category and calculate total amount


12)retrieve the sales amount for each category

Query: select category,sum(sales_amount) from sales group by category;

Output:

13)calculate the average sales for each product

Query: select product_id,avg(sales_amount) from sales group by product_id;

Output:

14) Find categories with total sales greater than $1000

Query: select category,sum(sales_amount) from sales group by category having


sum(sales_amount)>1000;

Output:

15)Retrieve the highest sales amount for each category

Query: select category,max(sales_amount) from sales group by category;

Output:
16) Suppose you have a sales table with colums product_id, category, and sales_amount, and you want
to find the total sales for each product category, but only for categories wit total sales greater than $500

Query: select category,sum(sales_amount) from sales group by category having


sum(sales_amount)>500;

Output:

4.Create table Teacher and admin and insert records in the table

Table: Teacher Table: Admin

a)To display the name of the teacher and their designation

Query: select tname,designation from teacher natural join admin;

Output:
b)TO display the names and subject of all female teachers

Query: select tname,subject from teacher natural join admin where gender='female';

Output:

c)To display the details of all teachers including gender and designation

Query: select * from teacher natural join admin;

Output:

d)To display details of all HOD

Query: select * from teacher natural join admin where designation='hod';

Output:

5.Create the table Emp with the following constraints

Field Name Data Type Size Constraint


ID int 2 Primary Key
Name Varchar 20 Not NULL
Age Int 3
Address Char 40
Salary Int 7
Phone Char 15
Add the following records to the above table ‘Emp’ and write commands in SQL

a)To display list of all employees below 25 years old

Query: select name from emp where age<25;

Output:

b) To list names and respective salaries in descending order of salary

Query: select name, salary from emp order by salary desc;

Output:

c)To count the number of employees with names starting with ‘k’

Query: select count(*) from emp where name like ‘k%’;

Output:
d)to List names and addresses of those people who have ‘Delhi’ in their address

Query: select name, address from emp where address like ‘%Delhi’;

Output:

e)To list out all the employees whose salary is in between 70000 and 80000

Query: select name from emp where salary between 70000 and 80000;

Output:

f)Count number of employees based on their age group

Query: select age, count(age) from emp group by age;

Output:
g)To list out all the employee names whose phone number starts with ‘99’

Query: select name from emp where phone like ‘99%’

Output:

h)To display all the details of employee whose salary is above 85000

Query: select * from emp where salary>85000;

Output:

i)To display all the employee names whose name contains the character ‘a’ anywhere

Query: select name from emp where name like ‘%a%’;

Output:
6. Write SQL commands for (a) to (i) on the basis of teacher relation given below

Table: Teacher1

No. Name Age Department Date of Join Salary Sex


1 Jigal 34 Computer 10/01/97 12000 M
2 Sharmila 31 History 24/03/98 20000 F
3 Sandeep 32 Maths 12/12/96 30000 M
4 Sangeeta 35 History 01/07/99 40000 F
5 Rakesh 42 Maths 05/09/97 25000 M
6 Shyam 50 History 27/02/97 30000 M
7 Shiv Om 44 Computer 25/02/97 21000 M
8 Shalakha 33 Maths 31/07/97 20000 F

a)To show all information about the teacher of history department.

Query: select * from teacher1 where department=’history’;

Output:

b)To list the names of female teachers who are in maths department.
Query: Select name from teacher1 where sex='F' and department='maths';

Output:

c)To list names of all teachers with their date of joining in ascending order

Query: select name,DateofJoin from teacher1 order by DateofJoin;

Output:

d)To display teacher name, salary, age from male teachers only.

Query: select name, salary, age from teacher1 where sex= ‘M’;

Output:
e)To count the number of teachers with age>23.

Query: select count(*) from teacher1 where age>23;

Output:

f)To insert a new row in the TEACHER1 table with the following data:

9, ‘Raja’, 26, ‘Computer’, 13/05/95, 2300, ‘M’.

Query: insert into teacher1 values(9, ‘Raja’, 26, ‘Computer’, ‘1995-05-13’, 2300, ‘M’);

Output:

g)To show all information about the teachers in this table.

Query: select * from teacher;

Output:
h)Arrange the whole table in the alphabetical order to name.

Query: select * from teacher1 order by name;

Output:

i)Display the age of the teachers whose name starts with ‘S’.

Query: select name, age from teacher1 where name like ‘S%’;

Output:
7.

Table: Student1

a)Select all the Nonmedical stream students from STUDENT1

Query: select name from student1 where stream='nonmedical';

Output:
b) List all the names of those students who are in class 12 sorted by stipend

Query: select name,stipend from student1 where class like '12%' order by stipend;

Output:

c) List all students sorted by AvgMark in descending order

Query: select name,avdmark from student1 order by avdmark desc;

Output:

You might also like