0% found this document useful (0 votes)
10 views

SQL_Question_33_Complete_Solution (1)

Uploaded by

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

SQL_Question_33_Complete_Solution (1)

Uploaded by

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

QUESTION 33 -Create a table STUDENT with the under-mentioned structure

using SQL Statement:


- StdID: Number, Primary Key
- StdName: Character(30), NOT NULL
- Sex: Character(6), Male or Female
- Percentage: Number
- SClass: Number
- Sec: Character
- Stream: Character(10), Science or Commerce
- DOB: Date

a) Insert 10 records into the table

SQL Query:

INSERT INTO STUDENT (StdID, StdName, Sex, Percentage, SClass, Sec, Stream, DOB)
VALUES
(1, 'John Smith', 'Male', 85, 12, 'A', 'Science', '2005-06-20'),
(2, 'Mary Adams', 'Female', 75, 11, 'B', 'Commerce', '2006-07-15'),
(3, 'Sam Watson', 'Male', 90, 12, 'A', 'Science', '2005-05-10'),
(4, 'Emily Jones', 'Female', 82, 10, 'C', 'Commerce', '2007-08-22'),
(5, 'Harry White', 'Male', 65, 11, 'A', 'Science', '2006-04-18'),
(6, 'Clara Brown', 'Female', 92, 12, 'B', 'Science', '2005-11-11'),
(7, 'Kevin Grey', 'Male', 45, 10, 'C', 'Commerce', '2007-12-04'),
(8, 'Lily Black', 'Female', 78, 11, 'A', 'Commerce', '2006-10-27'),
(9, 'George Blue', 'Male', 55, 12, 'A', 'Science', '2005-03-03'),
(10, 'Nina Green', 'Female', 88, 10, 'B', 'Science', '2007-01-05');

OUTPUT

b) Display the names and class of all students

SQL Query:

SELECT StdName, SClass FROM STUDENT;

OUTPUT
c) Display the details of all students scoring above 80%

SQL Query:

SELECT * FROM STUDENT


WHERE Percentage > 80;

OUTPUT

d) Display the names of all students from the Science stream arranged by
percentage

SQL Query:

SELECT StdName, Percentage FROM STUDENT


WHERE Stream = 'Science'
ORDER BY Percentage DESC;

OUTPUT
e) To add a column TeacherID in the table

SQL Query:

ALTER TABLE STUDENT


ADD TeacherID Number;
OUTPUT

f) To remove the column TeacherID

SQL Query:

ALTER TABLE STUDENT


DROP COLUMN TeacherID;

OUTPUT

g) To display the details of all students who are not in Science stream

SQL Query:

SELECT * FROM STUDENT


WHERE Stream != 'Science';

OUTPUT
h) To increase the percentage of all students by 2, who are having percentage
less than 40

SQL Query:

UPDATE STUDENT
SET Percentage = Percentage + 2
WHERE Percentage < 40;

OUTPUT

i) To display all the streams from the table

SQL Query:

SELECT DISTINCT Stream


FROM STUDENT;

OUTPUT

j) To display the number of students in each stream

SQL Query:

SELECT Stream, COUNT(*) as NumStudents


FROM STUDENT
GROUP BY Stream;

OUTPUT
k) To display the total number of male and female students

SQL Query:

SELECT Sex, COUNT(*) as TotalStudents


FROM STUDENT
GROUP BY Sex;

OUTPUT

QUESTION 34 Create a table 2 store employee details are shown below and
right SQL command for the following .

Write a query to display EName and Sal of employees whose salary are greater
than or equal to 2200?

SQL QUERY

SELECT EName, Sal FROM Empl


WHERE Sal >= 2200;

OUTPUT
b. Write a query to display details of employees who are not getting
commission?

SQL QUERY

SELECT * FROM Empl


WHERE Comm IS NULL;

OUTPUT

c. Write a query to display employee name and salary of those employees who
don't have their salary in range of 2500 to 4000?

SQL QUERY

SELECT EName, Sal FROM Empl


WHERE Sal NOT BETWEEN 2500 AND 4000;

OUTPUT

d. Write a query to display the name, job title and salary of employees who
don't have a manager?

SQL QUERY

SELECT EName, Job, Sal FROM Empl


WHERE Mgr IS NULL;

OUTPUT
e. Write a query to display the name of employee whose name contains 'L' as
third alphabet?

SQL QUERY

SELECT EName FROM Empl


WHERE EName LIKE '__L%';

OUTPUT

f. Write a query to display the name of employee whose name contains 'T' as
last alphabet?

SQL QUERY

SELECT EName FROM Empl


WHERE EName LIKE '%T';

OUTPUT

g. Write a query to display the name of employee whose name contains 'K' as
First and 'L' as third alphabet?

SQL QUERY

SELECT EName FROM Empl


WHERE EName LIKE 'K_L%';

OUTPUT
QUESTION 35 Write SQL command to get the following:

(a) Show the minimum, maximum, and average salary of managers.

SQL QUERY

SELECT MIN(Sal) AS MinSalary, MAX(Sal) AS MaxSalary, AVG(Sal) AS AvgSalary


FROM EMP
WHERE Designation = 'MANAGER';

OUTPUT

(b) Show the number of clerks in the organization.

SQL QUERY

SELECT COUNT(*) AS NumberOfClerks FROM EMP


WHERE Designation = 'CLERK';

OUTPUT

(c) Display the designation-wise list of employees with name, salary, and date
of joining.

SQL QUERY

SELECT Designation, EmpName, Sal, DOJ FROM EMP


ORDER BY Designation;
OUTPUT

(d) List the name of employees who are not getting commission.

SQL QUERY

SELECT EmpName FROM EMP


WHERE Comm IS NULL;

OUTPUT

(e) Show the average salary of employees grouped by DeptID.

SQL QUERY

SELECT DeptID, AVG(Sal) AS AvgSalary FROM EMP


GROUP BY DeptID;

OUTPUT
(f) Display the name of employees along with their designation and department
name.

SQL QUERY

SELECT EmpName, Designation, DeptName FROM EMP e


JOIN DEPT d ON e.DeptID = d.DeptID;

OUTPUT

(g) Show the number of employees working in the ACCOUNTS department.

SQL QUERY

SELECT COUNT(*) AS NumberOfEmployees FROM EMP


JOIN DEPT d ON e.DeptID = d.DeptID
WHERE d.DeptName = 'ACCOUNTS';
QUESTION 36 -Write SQL commands for (i) to (vi) and write output for (vii) on
the basis of PRODUCTS relation given below:

(i) To show details of all PCs with stock more than 110.

SQL QUERY

SELECT * FROM PRODUCTS


WHERE PNAME = 'PC' AND STOCK > 110;

OUTPUT

(ii) To find the company which gives a warranty of more than 2 years.

SQL QUERY

SELECT DISTINCT COMPANY FROM PRODUCTS


WHERE WARRANTY > 2;

OUTPUT
(iii) To list stock value of the BPL company where stock value is the sum of the
products of price and stock.

SQL QUERY

SELECT SUM(PRICE * STOCK) AS StockValue FROM PRODUCTS


WHERE COMPANY = 'BPL';

OUTPUT

(iv) To show the number of products from each company.

SQL QUERY

SELECT COMPANY, COUNT(*) AS NumberOfProducts FROM PRODUCTS


GROUP BY COMPANY;

OUTPUT

(a) SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT;

(b) SELECT MAX(PRICE) FROM PRODUCT


WHERE WARRANTY<=3;
QUESTION 36- WRITE output for on the basis of products relation given below

(a) To list the details of furniture whose price is more than 5000:

SQL QUERY

SELECT * FROM FURNITURE


WHERE Price > 5000;

OUTPUT

(b) To list the name and price of furniture where the discount is between 10
and 20:

SQL QUERY

SELECT Item, Price FROM FURNITURE


WHERE Discount BETWEEN 10 AND 20;

OUTPUT

(c) To display the record of items where discount is 10:

SQL QUERY

SELECT * FROM FURNITURE


WHERE Discount = 10;

OUTPUT
(d) To list the item names and types of all items whose names start with 'P':

SQL QUERY

SELECT Item, Type FROM FURNITURE


WHERE Item LIKE 'P%';

OUTPUT

(E) to display price of babycot

SQL QUERY

SELECT*FROM FURNITURE
WHERE TYPE=’BABYCOT’;

OUTPUT

SELECT DISTINCT TYPE FROM FURNITURE;

SELECT COUNT(8) FROM FURNITURE WHERE DISCOUNT<25;


QUESTION 37- write SQL command/output for the following on the basis of the
given table graduate

(i) List the names of those students who have obtained rank 1 sorted by NAME:

SQL QUERY

SELECT Name FROM GRADUATE


WHERE Rank = 1
ORDER BY Name;

OUTPUT

(ii) Display a list of all those names whose AVERAGE is greater than 65:

SQL QUERY

SELECT Name FROM GRADUATE


WHERE Average > 65;

OUTPUT
(iii) Display the names of those students who have opted COMPUTER as a
SUBJECT with an AVERAGE of more than 60:

SQL QUERY

SELECT Name FROM GRADUATE


WHERE Subject = 'COMPUTER' AND Average > 60;

OUTPUT

(iv) List the names of all the students in alphabetical order:

SQL QUERY

SELECT Name FROM GRADUATE


ORDER BY Name;

OUTPUT

(v) SELECT Name FROM GRADUATE


WHERE Name LIKE 'I%';

OUTPUT

(vi)SELECT DISTINCT Rank


FROM GRADUATE;

OUTPUT
QUESTION 38- Answer the questions based on the table given below

(a) To list the names of all patients admitted after 1998-01-15:

SQL QUERY

SELECT Name FROM HOSPITAL


WHERE Dateofadm > '1998-01-15';

OUTPUT

(b) To list the names of female patients who are in the ENT department:

SQL QUERY

SELECT Name FROM HOSPITAL


WHERE Sex = 'F' AND Department = 'ENT';
OUTPUT

(c) To list the names of all patients with their date of admission in ascending
order:

SQL QUERY

SELECT Name, Dateofadm FROM HOSPITAL


ORDER BY Dateofadm ASC;

OUTPUT

(d) To display patient Name, Charges, and Age for only female patients:

SQL QUERY

SELECT Name, Charges, Age FROM HOSPITAL


WHERE Sex = 'F';

OUTPUT
(e) Execute the output of the following SQL commands:

(i) SELECT COUNT(DISTINCT Charges) FROM HOSPITAL;

(ii) SELECT MIN(Age) FROM HOSPITAL


WHERE Sex = 'F';

You might also like