0% found this document useful (0 votes)
4 views5 pages

SQL Queries for Practical File

The document contains instructions for writing SQL queries in a practical file, divided into two parts: MySQL Queries-1 and MySQL Queries-2. Each part includes a table structure and a series of SQL queries related to employee and worker data, covering operations such as creating tables, inserting data, and retrieving information based on various conditions. Specific formatting guidelines are provided for writing the queries and tables in the file.

Uploaded by

nikhil.palwar09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

SQL Queries for Practical File

The document contains instructions for writing SQL queries in a practical file, divided into two parts: MySQL Queries-1 and MySQL Queries-2. Each part includes a table structure and a series of SQL queries related to employee and worker data, covering operations such as creating tables, inserting data, and retrieving information based on various conditions. Specific formatting guidelines are provided for writing the queries and tables in the file.

Uploaded by

nikhil.palwar09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Instructions:

• These SQL queries has to be in Computers’ Practical File Only.


• MySQL Queries are divided into 2 parts (MySQL Queries-1 and MySQL Queries-2), each part
should be started from the new page only.
• Table should be created on a blank (left hand side paper only).
• Black pen should be used for writing the question and Blue pen should be used for writing the
answers.

MySQL Queries - 1
Table - EmpInfo

EmpID EmpFname EmpLname Department Address DOB Gender

1 Sanjay Mehra HR Hyderabad(HYD) 1976-12-01 M

2 Ananya Mishra Admin Delhi(DEL) 1968-05-02 F

3 Rohan Diwan Account Mumbai(BOM) 1980-01-01 M

4 Sonia Kulkarni HR Hyderabad(HYD) 1992-02-05 F

5 Ankit Kapoor Admin Delhi(DEL) 1994-07-03 M

Q1. Write a query to create the above table as per the column names given (Take Datatypes as per your
requirement).
Ans. CREATE TABLE EmpInfo(EmpID INT Primary Key, EmpFname VARCHAR(30), EmpLname VARCHAR(25),
Deparment VARCHAR(20), Address VARCHAR(50), DOB date, Gender CHAR(1));

Q2. Write a query to insert a row. Consider the last row of the given table.
Ans. INSERT into EmpInfo Values (5, 'Ankit', 'Kapoor', 'Admin', 'Delhi(DEL) ', '1994-07-03', 'M');

Q3. Write a query to fetch the EmpFname FROM the EmpInfo table in upper case and use the ALIAS
name as EmpName.
Ans. SELECT UPPER(EmpFname) as 'EmpName' FROM EmpInfo;

Q4. Write a query to fetch the number of employees working in the department ‘HR’.
Ans. SELECT COUNT(*) FROM EmpInfo WHERE Department = 'HR';

Q5. Write a query to get the current date.


Ans. SELECT CURDATE();
Or
SELECT CURRENT_DATE;

Q6. Write a query to fetch all the details of Employees except whose EmpFname is Sanjay and Sonia.
Ans. SELECT * FROM EmpInfo WHERE EmpFname NOT IN ('Sanjay','Sonia');

Q7. Write a query to find the names of employees that begin with ‘S’
Ans. SELECT * FROM EmpInfo WHERE EmpFname LIKE 'S%';
Q8. Write a query to retrieve the EmpFname and EmpLname in a single column as “FullName”. The first
name and the last name must be separated with space.
Ans. SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmpInfo;

Q9. Write a query to display the employees whose DOB in between 1970-05-02 to 1975-12-31;
Ans. SELECT EmpFname, DOB FROM EmpInfo WHERE DOB BETWEEN '1970-05-02' AND '1975-12-31';

Q10. Write a query to fetch all the records FROM the EmpInfo table ordered by EmpLname in descending
order and Department in the ascending order.
Ans. SELECT * FROM EmpInfo ORDER BY EmpFname desc, Department asc;

Q11. Write a query to fetch details of employees whose EmpLname ends with an alphabet ‘A’ and
contains five alphabets only.
Ans. SELECT * FROM EmpInfo WHERE EmpLname LIKE '_ _ _ _ A’;

Q12. Write a query to fetch details of employees with the address as “DELHI(DEL)”.
Ans. SELECT * FROM EmpInfo WHERE Address LIKE '%DELHI(DEL)%';

Q13. Write a query to fetch whole details of employees


Ans. SELECT * FROM EmpInfo;

(DO THIS FROM NEXT NEW PAGE ONWARDS)


NOTE: (Spaces are given in name of some workers in First_Names colums for some queries. So, Make the Table as it
is shown in the pdf).

MySQL Queries - 2
Table – Worker

Worker_Id First_Name Last_Name Salary Joining_Date Project Department

1001 Monika Arora 100000 2014-02-20 P1 HR

1002 Niharika Verma 80000 2010-06-11 P2 Admin

1003 Vishal NULL 30000 2000-02-20 P3 HR

1004 Amitabh Singh 50000 2008-02-20 P1 Admin

1005 Vikram Arora 500000 2006-06-11 P2 Admin

1006 Vipul Diwan 200000 2012-08-10 P3 IT

1007 Satish NULL 75000 2010-06-20 P1 Account

1008 Geetika Chauhan 90000 2015-04-11 P3 IT

Q1. Write a SQL query to fetch First_Name, Salary and Incentive (which is Salary + 500) from table
Worker.
Ans. SELECT First_Name, Salary, Salary+500 as “Incentive” FROM Worker;
Q3. Write a query to find all the employees who are in HR, Admin and IT department.
Ans. SELECT * FROM EmpInfo WHERE Department IN(‘HR’, ‘Admin’, ‘IT’);

Q3. Write an SQL query to the Average Salary of all the workers from Worker table.
Ans. SELECT AVG(Salary) FROM Worker;

Q4. Write a SQL query to fetch unique values of Department from Worker table.
Ans. SELECT DISTINCT Department FROM Worker;

Q5. Write a SQL query to print the first three characters of First_Name FROM Worker table.
Ans. SELECT SUBSTRING(First_Name,1,3) FROM Worker;

Q6. Write a SQL query to find the position of the alphabet (‘a’) in the name ‘Amitabh’ given in
First_Name column from Worker table.
Ans. SELECT INSTR(First_Name, 'a') FROM Worker WHERE First_Name = 'Amitabh';

Q7. Write a SQL query to print the First_Name from Worker table after removing white spaces from the
trailing (right side).
Ans. SELECT RTRIM(First_Name) FROM Worker;

Q8. Write a SQL query to print the Department from Worker table after removing white spaces from the
leading (left side).
Ans. SELECT LTRIM(Department) FROM Worker;

Q9. Write a SQL query that fetches the unique values of Department from Worker table and prints its
length.
Ans. SELECT DISTINCT LENGTH(Department) FROM Worker;

Q10. Write a SQL query to print the First_Name and Last_Name from Worker Whose Last_Name is Null.
Ans. SELECT First_Name, Last_Name FROM Worker where Last_Name IS NULL;

Q11. Write a SQL query to print the First_Name and Department from Worker table into a single column
Name_Dept along with a string ‘is in’.
Ans. SELECT CONCAT(First_Name, ' is in ', Department) as ' Name_Dept ' FROM Worker;

Q12. Write a SQL query to print all Worker details from the Worker table sort Project in Ascending and
Department in Descending.
Ans. SELECT * FROM Worker ORDER BY Project ASC, Department DESC;

Q13. Write a SQL query to print all details of the workers whose Salary between 30000 to 70000 (Both
inclusive);
Ans. SELECT * FROM Worker WHERE Salary BETWEEN 30000 AND 70000;

Q14. Write a SQL query to print details of Workers with the first name as “Vipul” and “Satish” from
Worker table.
Ans. SELECT * FROM Worker WHERE First_Name IN ('Vipul','Satish');

Q15. Write a SQL query to print details of workers whose Salary exceeds from 100000 and Sort their
First_Name;
Ans. SELECT * FROM Worker WHERE Salary > 100000 ORDER BY First_Name;

Q16. Write a SQL query to print details of Workers who joined after 2010.
Ans. SELECT * FROM Worker WHERE Joining_Date > '2010-01-01';
OR
SELECT * FROM Worker WHERE YEAR(Joining_Date) > 2010;

Q17. Write a SQL query to print first 4 letters of the workers whose salary is less than 60000.
Ans. SELECT left(First_Name,4), Salary FROM Worker WHERE Salary < 60000;

Q18. Write a SQL query to print details of the Workers Joining Month is February.
Ans. SELECT * FROM Worker WHERE MONTHNAME(Joining_Date) = ‘February’;
OR
SELECT * FROM Worker WHERE MONTH(Joining_Date) = 2;

Q19. Write a SQL query to print Name and Joining Day Name of the Workers.
Ans. SELECT First_Name, Dayname(Joining_Date) FROM Worker;

Q20. Write a SQL query to print the Maximum, Minimun and Average Salary of each Project wise.
Ans. SELECT Project, MAX(Salary) as 'Max_Sal_Of_Dep', MIN(Salary) as 'Min_Sal_Of_Dep', AVG(Salary) as
'Average_Sal_Of_Dep' from Worker GROUP BY Project;

Q21. Write a SQL query to count the numbers of workers department wise and order by the numbers of
workers in descending order.
Ans. SELECT Department, count(Department) as 'Employees' from Worker GROUP BY Department ORDER
BY count(Department) desc;

Q22. Write a SQL query to print Maximum salary of Workers Department wise whose Maximum Salary is
greater than 50000.
Ans. SELECT Department, MAX(Salary) from Workers GROUP BY Department HAVING MAX(Salary) > 50000;

Q23. Write a SQL query to print details of the Workers who have joined in June 2010.
Ans. SELECT * FROM Worker WHERE year(Joining_Date) = 2010 AND month(Joining_Date) = 6;

Q24. Write a SQL query to fetch the no. of employees working in the department ‘Admin’.
Ans. SELECT Department, COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';

Table: Project_Location

Project Project_Loc Project_Deadline


P1 Delhi 2026-05-01
P2 Mumbai 2028-01-01
P3 Bangalore 2027-12-01

Q25. Write a SQL query to fetch the First_Name, Department, Project and Project_Loc from table Worker
and Project_Location. (without any Restriction)
Ans. SELECT First_Name, Department, Worker.Project, Project_Loc FROM Worker, Project_Location;

Q26. Write a SQL query to fetch the First_Name, Department, Project and Project_Loc from table Worker
and Project_Location by using Equi-Join.
Ans. SELECT First_Name, Department, Project, Project_Loc FROM Worker, Project_Location WHERE
Worker.Project = Project_Location.Project;
Q27. Write a SQL query to fetch the First_Name, Department, Project and Project_Loc from table Worker
and Project_Location by using Equi-Join and give the alias name to the tables.
Ans. SELECT First_Name, Department, PL.Project, Project_Loc FROM Worker W, Project_Location PL
WHERE W.Project = PL.Project;

******

You might also like