Dbs Lab 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Task: Create a simple employee database.

● Create a new database named "EmployeeDB".

Ans) - - creation of database


create database "EmployeeDB"

● Create a table named "Employees" with the following columns:

1. Employee ID (auto-incremented integer, primary key)


2. First Name (text)
3. Last Name (text)
4. Date of Birth (date)
5. Gender (text)
6. Department (text)

Ans) - - creation of table


create table "Employees"(Id int ,
EmployeeID nvarchar (100) ,
FirstName nvarchar (100) ,
LastName nvarchar (100) ,
DateofBirth date ,
Gender nvarchar(100) ,
Department nvarchar(100))

● Insert at least 5 sample records into the "Employees" table.

Ans) - - insert into


insert into "Employees" values (1, 156730 , 'Rehan', 'Malik', '16Feb1990' , 'Male' , 'SE')
insert into "Employees" values (2, 156731 , 'Rohan', 'Khan', '15Aug1999' , 'Male' , 'CS')
insert into "Employees" values (3, 156732 , 'Abdul', 'Haadi', '23Dec2000' , 'Male' , 'Sales')
insert into "Employees" values (4, 156733 , 'Ayesha', 'Noor', '06Oct1989' , 'Female' , 'BBA')
insert into "Employees" values (5, 156734 , 'Fatima', 'farooq', '10Jan2001' , 'Female' , 'MS')

● Write an SQL query to display all employees information.

Ans) - - select
select * from "Employees"
● Write an SQL query to display the employees who belong to the
"Sales" department.

Ans) - - select
select * from "Employees" where 'Sales' = Department

● Write an SQL query to display the employees information sorted by


their last names in ascending order.

Ans) - - select
select * from "Employees" ORDER BY LastName ASC

● Write an SQL query to update the department of an employee with a


specific Employee ID.

Ans) - - updation of record in database


UPDATE "Employees"
SET Department = 'IT'
WHERE EmployeeID = 156731

● Write an SQL query to delete an employee with a specific Employee


ID.

Ans) - - - SQL delete statement


DELETE FROM "Employees"
WHERE EmployeeID = 156734

● Write an SQL query to calculate the average age of all employees.

Ans) - - select
select avg (datediff (year,Dateofbirth,getdate())) from "Employees"

● Write an SQL query to create a new table named "Salary" with the
following columns:

1. Employee ID (integer)
2. Salary (float)

Ans) - - creation of table


create table "Salary"(Id int , EmployeeID nvarchar (100) , Salary nvarchar (100))

You might also like