0% found this document useful (0 votes)
76 views14 pages

SQL Practical Questions

The document contains practice questions for an M.Sc. Integrated Semester 5 course, focusing on SQL database creation and manipulation. It includes tasks such as creating a bank database, managing employee records, and executing various SQL queries for data retrieval and updates. Additionally, it covers concepts like SQL injection and provides examples of creating tables and inserting data.

Uploaded by

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

SQL Practical Questions

The document contains practice questions for an M.Sc. Integrated Semester 5 course, focusing on SQL database creation and manipulation. It includes tasks such as creating a bank database, managing employee records, and executing various SQL queries for data retrieval and updates. Additionally, it covers concepts like SQL injection and provides examples of creating tables and inserting data.

Uploaded by

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

M.Sc.

Integrated Sem 5

Practice Questions

Question 1:
 Create a database – Bank_database
 Create a table – employees ( empid, name, post, dept)
 empid should not contain null values and duplicates values
 empid values should be auto increment
 name, post and dept should not contain null values
 post should have default value as “Probation”
 Insert the following data
empid name post dept
1001 Pavan Manager Loan
1002 Ajay Cashier Cash
1003 Drashti Accountant Account
1004 Meet Associate Loan
1005 Khushi Associate Deposit
1006 Vishal Trainee Cash

 Display the whole data.


 Display empid and name.
 Update the post of Vishal to Cashier.
 Display the data.
empid name post dept
1001 Pavan Manager Loan
1004 Meet Associate Loan

 Update the following data.


empid name post dept
1001 Pavan Manager Loan
1002 Ajay Cashier Cash
1003 Drashti Accountant Account
1004 Meet Associate IT
1005 Khushi Associate Deposit
1006 Vishal Trainee Cash
 Delete the following data
1002 Ajay Cashier Cash

create database banks_database;

use banks_database;

CREATE TABLE employees (

empid INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100) NOT NULL,

post VARCHAR(100) NOT NULL DEFAULT 'Probation',

dept VARCHAR(100) NOT NULL

);

insert into employees values(1001,"Pavan","Manager","Loan"),

(1002,"Ajay","Chasier","Cash"),

(1003,"Drashti","Accountant","Account"),

(1004,"Meet","Associate","Loan"),

(1005,"Khushi","Associate","Deposit"),

(1006,"Vishal","Trainee","Cash");

select * from employees;

select empid,name from employees;

update employees set post = "Cashier" where name = "Vishal";

select * from employees where empid in(1001,1004);


update employees set post = "Trainee" where name = "Vishal";

update employees set dept = "IT" where name = "Meet";

delete from employees where empid = 1002 ;

Question 2:
I. Get all employees.
II. Display the first name and last name of all employees.
III. Display all the values of the “First_Name” column using
the alias “Employee Name”
IV. Get all “Last_Name” in lowercase.
V. Get unique “DEPARTMENT”.
VI. Get all “Last_Name” in uppercase.
VII. Get the first 4 characters of “FIRST_NAME” column.
VIII. Get the position of the letter ‘h’ in ‘John’.
IX. Get all values from the “FIRST_NAME” column after
removing white space on the right.
X. Get all values from the “FIRST_NAME” column after
removing white space on the left.
XI. Write the syntax to create the “employee” table.

create database banks_detail;

use banks_detail;

create table Employee(


Employee_id int,
First_Name varchar(100),
Last_Name varchar(100),
Salary decimal(10,2),
Joining_date date,
Department varchar(100)
);

insert into Employee values(1,"Bob","Kinto",1000000,"2019-01-


20","Finance"),
(2,"Jerry","kansxo",6000000,"2019-01-15","IT"),
(3,"Philip","Jose",8900000,"2019-02-05","Banking"),
(4,"John","Abraham",2000000,"2019-02-05","Insurance"),
(5,"Micheal","Mathew",2200000,"2019-02-28","Finance"),
(6,"Alex","Chreketo",4000000,"2019-05-10","IT"),
(7,"Yahan","Soso",1230000,"2019-06-20","Banking");

create table Reward(


Employee_id int,
Date_Reward date,
Amount int
);

insert into Reward values(1,"2019-05-11",1000),


(2,"2019-02-15",5000),
(3,"2019-04-22",2000),
(1,"2019-06-20",8000);

select * from Employee;


select * from Reward;

select First_Name,Last_Name From Employee;

select First_Name as "Employee Name" from Employee;

select lower(Last_Name) from Employee;

select distinct department from Employee;

select upper(Last_Name) from Employee;

select substring(First_Name,1,4) from Employee;

select instr("John","h");

select rtrim(First_Name) from Employee;


select ltrim(First_Name) from Employee;

CREATE TABLE employee (


Employee_id INT PRIMARY KEY,
First_Name VARCHAR(100),
Last_Name VARCHAR(100),
Salary DECIMAL(10, 2),
Joining_date DATE,
Department VARCHAR(100)
);

Question 3:
I. Get the length of the text in the “First_name” column.
II. Get the employee’s first name after replacing ‘o’ with ‘#’.
III. Get the employee’s last name and first name in a single
column separated by a ‘_’.
IV. Get the year, month, and day from the “Joining_date”
column.
V. Get all employees in ascending order by first name.
VI. Get all employees in descending order by first name.
VII. Get all employees in ascending order by first name and
descending order by salary.
VIII. Get employees whose first name is “Bob”.
IX. Get employees whose first name is “Bob” or “Alex”.
X. Get employees whose first name is neither “Bob” nor
“Alex”.
XI. What is SQL injection? - SQL injection is one of the
techniques used by hackers to hack a website by
injecting SQL commands into data fields.

use banks_detail;

create table Employee_1(


Employee_id int,
First_Name varchar(100),
Last_Name varchar(100),
Salary decimal(10,2),
Joining_date date,
Department varchar(100)
);

insert into Employee_1 values(1,"Bob","Kinto",1000000,"2019-01-


20","Finance"),
(2,"Jerry","kansxo",6000000,"2019-01-15","IT"),
(3,"Philip","Jose",8900000,"2019-02-05","Banking"),
(4,"John","Abraham",2000000,"2019-02-05","Insurance"),
(5,"Micheal","Mathew",2200000,"2019-02-28","Finance"),
(6,"Alex","Chreketo",4000000,"2019-05-10","IT"),
(7,"Yahan","Soso",1230000,"2019-06-20","Banking");

create table Reward_1(


Employee_id int,
Date_Reward date,
Amount int
);

insert into Reward_1 values(1,"2019-05-11",1000),


(2,"2019-02-15",5000),
(3,"2019-04-22",2000),
(1,"2019-06-20",8000);

select * from Employee_1;


select * from Reward_1;
select length(First_Name) from Employee_1;

select replace(First_Name,"o","#") from Employee_1;

SELECT CONCAT(Last_Name, '_', First_Name) AS Full_Name FROM


employee;
SELECT CONCAT(Last_Name, '_', First_Name) FROM employee;

select year(Joining_date) as YEAR,


month(Joining_date) as MONTH,
day(Joining_date) as DAY from Employee_1;

select * from Employee_1 order by First_Name asc;


select * from Employee_1 order by First_Name desc;

select * from Employee_1 order by First_Name asc, Salary desc;


select * from Employee_1 order by First_Name desc, Salary asc;

select * from Employee_1 where First_Name = "Bob";


select * from Employee_1 where First_Name in ("Bob","Alex");
select * from Employee_1 where First_Name not in ("Bob","Alex");

Question 4:
I. Get all the details about employees whose first name
begins with ‘B’.
II. Get all the details about employees whose first name
contains ‘o’.
III. Get all the details of the employees whose first name ends
with ‘n’.
IV. Get all the details about employees whose first name ends
with ‘n’ and contains 4 letters.
V. Get all the details about employees whose first name
begins with ‘J’ and contains 4 letters.
VI. Get all the details of employees whose salary is over
3,000,000.
VII. Get all the details about employees whose salary is less
than 3,000,000.
VIII. Get all the details about employees with a salary between
2,000,000 and 5,000,000.
IX. Get all the details about employees whose first name is
‘Bob’ or ‘Alex’.
X. Get all the details about employees whose joining year is
“2019”.

create table Employee_2(


Employee_id int,
First_Name varchar(100),
Last_Name varchar(100),
Salary decimal(10,2),
Joining_date date,
Department varchar(100)
);

insert into Employee_2 values(1,"Bob","Kinto",1000000,"2019-01-


20","Finance"),
(2,"Jerry","kansxo",6000000,"2019-01-15","IT"),
(3,"Philip","Jose",8900000,"2019-02-05","Banking"),
(4,"John","Abraham",2000000,"2019-02-05","Insurance"),
(5,"Micheal","Mathew",2200000,"2019-02-28","Finance"),
(6,"Alex","Chreketo",4000000,"2019-05-10","IT"),
(7,"Yahan","Soso",1230000,"2019-06-20","Banking");

create table Reward_2(


Employee_id int,
Date_Reward date,
Amount int
);

insert into Reward_2 values(1,"2019-05-11",1000),


(2,"2019-02-15",5000),
(3,"2019-04-22",2000),
(1,"2019-06-20",8000);
select * from Employee_2;
select * from Reward_2;

select * from Employee_2 where First_Name like "%B";


select * from Employee_2 where First_Name like "%O%";
select * from Employee_2 where First_Name like "%N";
select * from Employee_2 where First_Name like "%___n";
select * from Employee_2 where First_Name like "%J___";

select * from Employee_2 where Salary > 3000000;


select * from Employee_2 where Salary < 3000000;
select * from Employee_2 where Salary between 2000000 and
5000000;

select * from Employee_2 where First_Name in ("Alex","Bob");

select * from Employee_2 where year(Joining_date) = 2019;

Question 5:
I. Get all the details on employees whose participation
month (Joining_date) is “January”
II. Get all the details of the employees who joined before
March 1, 2019
III. Get all the details on employees who joined after March
31, 2019
IV. Get the date and time of the employee’s enrollment.
V. Get the first names of employees who have the character
‘%’. Example: ‘Jack%’.
VI. Get the employee name (Last_name) after replacing the
special character with white space.
VII. Get the employee’s department and total salary, grouped
by department.

create table Employee_3(


Employee_id int,
First_Name varchar(100),
Last_Name varchar(100),
Salary decimal(10,2),
Joining_date date,
Department varchar(100)
);

insert into Employee_3 values(1,"Bob","Kinto",1000000,"2019-01-


20","Finance"),
(2,"Jerry","kansxo",6000000,"2019-01-15","IT"),
(3,"Philip","Jose",8900000,"2019-02-05","Banking"),
(4,"John","Abraham",2000000,"2019-02-05","Insurance"),
(5,"Micheal","Mathew",2200000,"2019-02-28","Finance"),
(6,"Alex","Chreketo",4000000,"2019-05-10","IT"),
(7,"Yahan","Soso",1230000,"2019-06-20","Banking");

create table Reward_3(


Employee_id int,
Date_Reward date,
Amount int
);

insert into Reward_3 values(1,"2019-05-11",1000),


(2,"2019-02-15",5000),
(3,"2019-04-22",2000),
(1,"2019-06-20",8000);

select * from Employee_3;


select * from Reward_3;

select * from Employee_3 where Month(Joining_date) = 1;

select * from Employee_3 where Joining_date < "2019-03-01";

select * from Employee_3 where Joining_date > "2019-03-31";

select Joining_date from Employee_3;

select First_Name from Employee_3 where First_Name like "%";


#[Out of syllabus Don't have to do]

select replace(Last_Name,"%"," ") as Replace_Character from


Employee_3;
select replace(Last_Name,"%"," ") from Employee_3;

select Department, sum(Salary) as Total_Salary from Employee_3


group by Department;
select Department, sum(Salary) from Employee_3 group by
Department;

You might also like