BASE Queries

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

PRACTICAL ASSIGNMENT

Sub.: Information Technology


Std.: X (CBSE)
PART B : VOCATIONAL SKILLS
Unit-3 Database Management System
Session-6 Structured Query Language
____________________________________________________________
Consider the following Employee table and write the SQL queries for the following:

EmpID EmpName DOB City Salary


101 Harsh 11-03-2004 Surat 47000
102 Kalpesh 28-10-2006 Baroda 38000
103 Ritu 05-08-2006 Mumbai 52000
104 Satish 17-11-2004 Surat 41000
105 Hirva 30-05-2005 Mumbai 36000

Note: Whenever you write queries all the field names (EmpID, EmpName, DOB, City, Salary) are
case sensitive.

1. Create Employee table with the following fields and constraints.


EmpID, EmpName, DOB, City, Salary
• Set EmpID as a Primary key
• EmpName must be unique
• DOB not left empty
• Set City default to ‘Surat’
• Salary must be greater than 30000

Ans:
CREATE TABLE Employee
(
EmpID Integer PRIMARY KEY,
EmpName Varchar(30),
DOB date NOT NULL,
City Varchar(20) DEFAULT 'Surat',
Salary Decimal,
UNIQUE(EmpName),
CHECK(Salary>30000)
)

2. Insert 5 records into Employee table.

INSERT INTO Employee VALUES(101,'Harsh','2004-03-11','Surat',47000)


INSERT INTO Employee VALUES(102,'Kalpesh','2006-10-28','Baroda',38000)
INSERT INTO Employee VALUES(103,'Ritu','2006-08-05','Mumbai',52000)
INSERT INTO Employee VALUES(104,'Satish','2004-11-17','Surat',41000)
INSERT INTO Employee VALUES(105,'Hirva','2005-05-30','Mumbai',36000)

3. Insert 1 record having NULL value in the Salary.

INSERT INTO Employee VALUES(106,'Rajveer','2004-08-30','Surat',NULL)

4. Display the structure of Employee table.

DESC Employee
OR
DESCRIBE Employee

5. Display all the records from Employee table.

SELECT *
FROM Employee

6. Display only EmpName and Salary.

SELECT EmpName, Salary


FROM Employee

7. Display the record whose name is Ritu.

SELECT *
FROM Employee
WHERE EmpName='Ritu'

8. Display all the records whose DOB greater than '25-12-2005'.

SELECT *
FROM Employee
WHERE DOB>'2005-12-25'

Note: With Select query date always write in 'YYYY-MM-DD' format.

9. Display all the records whose City is 'Mumbai' and Salary is less than or equal 40000.

SELECT *
FROM Employee
WHERE City='Mumbai' and Salary<=40000

10. Display all the records whose City is 'Mumbai' or Salary is less than or equal 40000.

SELECT *
FROM Employee
WHERE City='Mumbai' or Salary<=40000
11. Display all the unique cities from City field.

SELECT DISTINCT(City)
FROM Employee

12. Display all the cities from City field.

SELECT ALL(City)
FROM Employee

13. Give 10% bonus to all the employee and display EmpName, Salary, Bonus, Total
Salary(Salary+Bonus).

SELECT EmpName, Salary, Salary*10/100 as "Bonus", Salary+( Salary*10/100) as "Total


Salary"
FROM Employee

14. Display the details of all those employees whose salary between 45000 to 55000.

SELECT *
FROM Employee
WHERE Salary BETWEEN 45000 AND 55000

15. Display the details of all those employees who are staying in Surat or Mumbai city.

SELECT *
FROM Employee
WHERE City IN('Surat','Mumbai')
OR
SELECT *
FROM Employee
WHERE City='Surat' OR City='Mumbai'

16. Display the details of all those employees who are not staying in Surat or Mumbai city.

SELECT *
FROM Employee
WHERE City NOT IN('Surat','Mumbai')

17. Display the details of all those employees whose name start with H character.

SELECT *
FROM Employee
WHERE EmpName LIKE 'H%'
18. Display the details of all those employees whose name end with H character.

SELECT *
FROM Employee
WHERE EmpName LIKE '%h'

19. Display the details of all those employees whose Salary contain NULL.

SELECT *
FROM Employee
WHERE Salary IS NULL

20. Update the EmpName to Sagar whose EmpID is 104.

UPDATE Employee
SET EmpName='Sagar'
WHERE EmpID=104

21. Increase the Salary of all employees by 3000.

UPDATE Employee
SET Salary=Salary+3000

22. Delete the details of those employee whose EmpID is 106.

DELETE
FROM Employee
WHERE EmpID=106

23. Delete the details of all employees.

DELETE
FROM Employee

24. Delete the table Employee.

DROP TABLE Employee


OR
DROP TABLE IF EXISTS Employee

25. Create two tables named Customer (CustID, Name) and Orders (OrderID, OrderDate,
CustID, Amount).

26. Make CustID as Primary key in Customer table and OrderID as Primary key in Orders table.

ALTER TABLE Customer


ADD PRIMARY KEY (CustID)
ALTER TABLE Orders
ADD PRIMARY KEY (OrderID)

27. Make CustID as Foreign key in Orders table.

ALTER TABLE Orders


ADD FOREIGN KEY (CustID) REFERENCES Customer (CustID)

28. Insert one field named Contact in Customer table.

ALTER TABLE Customer


ADD (Contact char(10))

29. Modify size of Name field to 50 in Customer table.

ALTER TABLE Customer


MODIFY (Name varchar(50))

30. Change the column name Name to CustName in Customer table.

ALTER TABLE Customer


CHANGE Name CustName varchar(50)

31. Insert the details of those branches into branch1 whose gross is greater than 7000 in
branch2.

INSERT INTO branch1


SELECT * FROM branch2
WHERE gross>7000

32. Display the current system date.

SELECT curdate()

33. Calculate area of circle where pi=3.14 and radius=6.

SELECT 3.14*6*6

34. Calculate Simple Interest where p=100000, r=5.5, t=2.

SELECT (10000*5.5*2)/100

35. Calculate Square and Cube of 10.

SELECT 10*10

SELECT 10*10*10

You might also like