You are on page 1of 28

DBMS LAB

LAB 3
Learning row commands (part 1)

Name : Burhan Ahmed Satti


Enrollment No. : 01-134172-065
Class : BSCS 4-A
Subject : DBMS Lab
Lab Assistant : Ramsha Baig

Objectives
•Learn how to concatenate row data
•Learn the use of
•Learn the use of >, <, != and = for selecting rows in table
•Learn to use AND keyword
•Learn to use between command

Tools Used
•SQL+
BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.1


Provide the empno and employee name whose salary is greater than 10,00
Solution
select empno, ename from emp where sal > 100;
Result

Conclusion
Employees with salaries higher than 1000 are shown.

DBMS Lab Page 2 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.2


List all employees, sorted by job.
Solution
select * from emp order by job;
Result

Conclusion
Employees are shown w.r.t their jobs.

DBMS Lab Page 3 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.3


List departments which are in Chicago
Solution
select * from dept where loc = ‘CHICAGO’;
Result

Conclusion
Departments of Chicago is shown.

DBMS Lab Page 4 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.4


Which employees get salary less than 20,000 and commission greater than 100.
Solution
select * from emp where sal < 20000 and comm > 100;
Result

Conclusion
Employees with commission greater than 100 are shown.

DBMS Lab Page 5 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.5


List the names and salary of all employees, except for Jones.
Solution
select ename, sal from emp where ename != ‘JONES’;
Result

Conclusion
Employee names except Jones are shown.

DBMS Lab Page 6 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.6


Find the empno, job, and hire date of all Clerks and managers hired before 1990. (use IN)
Solution
select empno, job, hiredate from emp where job in(‘CLERK’, ‘MANAGER’) and hiredate <
‘1-JAN-1990’;
Result

Conclusion
Employees of clerk and manager category which were employed before 1990 are shown.

DBMS Lab Page 7 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.7


Find the empno, job, and hire date of all Clerks and managers hired before 1990. (don’t use IN)
Solution
select empno, job, hiredate from emp where job = ‘CLERK’ or job = ‘MANAGER’ and hire-
date < ‘1-JAN-1990’;
Result

Conclusion
Employees of clerk and manager category which were employed before 1990 are shown.

DBMS Lab Page 8 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.8


List the numbers of all employees who do not earn commission.
Solution
select empno from emp where comm is null or comm = 0;
Result

Conclusion
Employees with no commission are shown.

DBMS Lab Page 9 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.9


List the departments except research. Without using like
Solution
select * from dept where dname != 'RESEARCH';
Result

Conclusion
All departments except research are shown;

DBMS Lab Page 10 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.10


List departments which are in Boston
Solution
select * from dept where loc = 'BOSTON';
Result

Conclusion
Only Operations department is n Boston.

DBMS Lab Page 11 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.11


Display the Employee name and designation in one column.
Solution
select concat(ename, job) from emp;
Result

Conclusion
Employee names and designations are concatenated.

DBMS Lab Page 12 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.12


Display all records from table whose name is Smith using all uppercases in where clause.
Solution
select * from emp where ename = upper('smith');
Result

Conclusion
Smith’s record is shown.

DBMS Lab Page 13 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.13


Display all records from table whose name is Smith using first letter captializaed in where
clause.
Solution
select * from emp where initcap(ename) = 'Smith';
Result

Conclusion
Smith’s record is used.

DBMS Lab Page 14 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.14


Display all records from table whose name is Smith using all lowercases in where clause.
Solution
select * from emp where lower(ename) = 'smith';
Result

Conclusion
We can now search data using small letters.

DBMS Lab Page 15 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.15


Display all records from table whose name is Smith using lower and upper cases both. i.e.
SMITH, smith both should be acceptable.
Solution
select * from emp where lower(ename) = 'smith' or upper(ename) = 'smith';
Result

Conclusion
Now searching can be done using upper or small case letters.

DBMS Lab Page 16 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.16


Show the result of those employees whose name length exceeds 5.
Solution
select * from emp where length(ename) > 5;
Result

Conclusion
Names of length greater than 5 are shown.

DBMS Lab Page 17 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.17


Display ename, salary and concatenated result of deptno and empno from table EMP where sal-
ary is less than 2500 or job is equal to MANAGER.
Solution
select ename, sal, concat(deptno, empno) from emp where sal > 2500 or job = upper('man-
ager');
Result

Conclusion
Managers and employees with salaries more than 2500 are shown.

DBMS Lab Page 18 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.18


Display first 3 characters of ename, deptno and job from table EMP where second character of
ename is ‘A’ and deptno is 30 or job is SALESMAN.
Solution
select substr(ename, 1, 3), substr(deptno, 1, 3), substr(job, 1, 3) from emp where substr(ename,
2, 1) = 'A' and deptno = 30 or job = upper('salesman');
Result

Conclusion
First 3 letters of employees with ‘a’ in their second name are shown.

DBMS Lab Page 19 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.19


Right Append dollar sign in Employee table to make salary 8 digit value.
Solution
select ename, rpad(sal, 8, '$') from emp;
Result

Conclusion
Salary is comprises of 8 digits now.

DBMS Lab Page 20 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.20


Calculate annual salary and rename it as PER_ANNUM_SALARY of the employee whose
ename is KING and salary is greater than 1500 from table EMP.
Solution
select ename, sal * 12 as "PER_ANNUM_SAL" from emp where ename = upper('King') and
sal > 1500;
Result

Conclusion
King’s annual salary is 60,000.

DBMS Lab Page 21 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.21


Round 345.6665 upto 2 digits from table DUAL.
Solution
select round(345.6665, 2) from dual;
Result

Conclusion
Value is rounded.

DBMS Lab Page 22 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.22


Members of the HR department want to have more flexibility with the queries that you are writ-
ing. They would like a report that displays the name and salary of employees who earn more than
an amount that the user specifies after a prompt
Solution
select empno, ename, sal from emp where sal > &salary;
Result

Conclusion
Search query is based on the variable and now can be modified in real-time.

DBMS Lab Page 23 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.23


The HR department wants to run reports based on a manager. Create a query that prompts the
user for a manager ID and generates the employee ID, name, salary, and department for that man-
ager’s employees. The HR department wants the ability to sort the report on a selected column.
You can test the data with the following values: manager ID = 103, sorted by employee name: man-
ager ID = 201, sorted by salary:
Solution
select empno, ename, sal, job from emp where mgr = &Manager order by &SortingVariable;
Result

Conclusion
Now the query is dynamic and it is changing values in real-time.

DBMS Lab Page 24 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.24


Write a query that displays the name (with the first letter uppercase and all other letters lower-
case) and the length of the name for all employees whose name starts with the letters J, A, or M.
Give each column an appropriate label. Sort the results by the employees’ names.
Solution
select initcap(ename), length(ename) from emp where substr(ename, 1, 1) in('J', 'A', 'M');
Result

Conclusion
names are shown with capital initial letter and which start with a, j or m.

DBMS Lab Page 25 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.25


Write a query to display the current date. Label the column Date.
Solution
select sysdate as "Date" from dual;
Result

Conclusion
System date is shown.

DBMS Lab Page 26 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.26


The HR department needs a report to display the employee number, name, salary, and salary
increased by 15.5% (expressed as a whole number) for each employee. Label the column New Sal-
ary.
Solution
select empno, ename, sal, sal * 15.5 as "New Salary" from emp;
Result

Conclusion
New Salaries with increment of 15.5% is shown.

DBMS Lab Page 27 of 28


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 3.27


Write a query that produces the following for each employee:
<employee name> earns <salary> monthly but wants <3 times salary>. Label the column
Dream Salary
Solution
select ename || ' earns ' || sal || ' monthly but wants ' || 3 * sal as "Dream Salary" from emp;
Result

Conclusion
String is shown as desired.

DBMS Lab Page 28 of 28

You might also like