0% found this document useful (0 votes)
36 views12 pages

assignment2

The document outlines SQL commands for creating and managing employee, department, and student tables, including data insertion, updates, and constraints. It also includes various SQL queries for data retrieval, such as filtering, counting, and manipulating string values. Additionally, it demonstrates how to handle duplicates and perform operations like union, intersect, and minus on student records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views12 pages

assignment2

The document outlines SQL commands for creating and managing employee, department, and student tables, including data insertion, updates, and constraints. It also includes various SQL queries for data retrieval, such as filtering, counting, and manipulating string values. Additionally, it demonstrates how to handle duplicates and perform operations like union, intersect, and minus on student records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Create an Employee table with EMP_ID, EMP_NAME, DEPT_ID, SAL, LOCATION Columns
and
Dept table with DEPT_ID, DEPT_NAME columns AND Student table with STUDENT_NO,
STUDENT_NAME, MARKS.

SQL> create table employee_details2(emp_id number, emp_name varchar2(100), dept_sal


int, locations varchar2(100));

Table created.

SQL> create table dept_details(dept_id number, dept_name varchar2(100));

Table created.

SQL> create table student_dets(stud_no number, stud_name varchar2(100), marks


number);

Table created.

2. Once the table created for Student, we need to add subject column to the table.

SQL> alter table student_dets add subject varchar2(100);

Table altered.

3. Write a statement to only remove the subject column

SQL> alter table student_dets drop column subject;

Table altered.

4. Once the Student table created, then add a constraint on marks column and it
should accept the
values only between 0 and 100.
5. Add a Constraint on STUDENT_NO Column; we should not pass Null value to it.
6. Insert the Sample data into EMP, DEPT and STUDENT tables.

insert into student_dets values( 1, 'shagufta', 50, 'physics');

1 row created.

SQL> insert into student_dets values( 2, 'nooreen', 60, 'maths');

1 row created.

sql> insert into dept_details values( 22, 'humanresource');

1 row created.

7. Update the commission 5000INR in the salary column to all the employees.

SQL> update employee_details2 set dept_sal = 5000 where emp_name = 'shagufta';

1 row updated.

8. First insert the data with Dept_Name as ‘Human Resource’ in DEPT table then
after write the
statement to Update the Dept_Name from ‘Human Resource’ to ‘HR’.
SQL> update dept_details set dept_name = 'hr' where dept_id = 22;

1 row updated.

9. Insert the data for ‘Sales’ Department in Dept and EMP table then after write
the statement to
Delete the employees belongs to ‘Sales’ department in EMP table.

SQL> insert into dept_details values( 23, 'sales');

1 row created.

SQL> delete dept_details where dept_name = 'sales';

1 row deleted.

10. Write an SQL query to print the first three characters of EMP_NAME from EMP
table.

SQL> select substr ('shagufta',1,3) from employee_details2;

SUB
---
sha

11. Write an SQL query to print details of the EMP and the EMP_NAME contains 'R'.
SQL> insert into employee_details2 values ( 2, 'ramesh', 20000, 'hyd');

1 row created.

SQL> select * from employee_details2 where emp_name like 'r%';

EMP_ID
----------
EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL
----------
LOCATIONS
--------------------------------------------------------------------------------
2
ramesh
20000
hyd

12. Write an SQL query to print details of the EMP whose FIRST_NAME ends with ‘h’
and contains
six alphabets. Ex: Ramesh
SQL> select * from employee_details2 where emp_name like '%h';

EMP_ID
----------
EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL
----------
LOCATIONS
--------------------------------------------------------------------------------
2
ramesh
20000
hyd

13. Write an SQL query to print details of the EMP whose SALARY lies between 5000
and 20000
SQL> select * from employee_details2 where dept_sal between 5000 and 20000;

EMP_ID
----------
EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL
----------
LOCATIONS
--------------------------------------------------------------------------------
1
shagufta
5000
hyd

EMP_ID
----------
EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL
----------
LOCATIONS
--------------------------------------------------------------------------------
2
ramesh
20000
hyd

EMP_ID
----------
EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL
----------
LOCATIONS
--------------------------------------------------------------------------------
3
suresh
10000
hyd

14. Write an SQL query to fetch the count of employees working in the department
‘HR’.

SQL> select count(dept_name) from dept_details;

COUNT(DEPT_NAME)
----------------
1
15. Write an SQL query to find people who have the same salary

16. Write an SQL query to fetch departments along with the total salaries paid for
each department.

SQL> select sum(dept_sal) from employee_details2;

SUM(DEPT_SAL)
-------------
35000

17. Write an SQL query to print the EMP_NAME after replacing “S” with “s”

SQL> select upper('shagufta') as upper_func from employee_details2;

UPPER_FU
--------
SHAGUFTA
SHAGUFTA
SHAGUFTA

18. Write an SQL query to fetch duplicate records having matching data in EMP_NAME
of a table.

SQL> select emp_id from employee_details2 group by emp_id having count (emp_id) >1;

EMP_ID
----------
3
19. Write an SQL query to fetch three max salaries from a table of employee.
SQL> select max(dept_sal) from employee_details2;

MAX(DEPT_SAL)
-------------
20000
20. Write an SQL query to fetch the greatest salary
SQL> select greatest(dept_sal) from employee_details2;

GREATEST(DEPT_SAL)
------------------
5000
20000
10000
10000
20. Write an SQL query to fetch the least salary

SQL> select least(dept_sal) from employee_details2;

LEAST(DEPT_SAL)
---------------
5000
20000
10000
10000

22. Write an SQL query to trim off the unwanted characters from left side like
remove ‘CO’ from
‘COMPUTER’
SQL> select ltrim('computer','co') from dual;

LTRIM(
------
mputer

23. Write a SQL query to fetch the Salary notify as


• less than 10000 should display as ‘Low’
• Between 10000 and 80000 as ‘Medium’
• Above 80000 as ‘High’

24. Write a SQL Query to fetch last day of the current month.
SQL> select last_day(to_date('2024/01/01','yyyy/mm/dd')) from dual;

LAST_DAY(
---------
31-JAN-24

25. Write a SQL Query to fetch the Current Year


SQL> select sysdate from dual;

SYSDATE
---------
02-JAN-24

26. Write a SQL Query to fetch the Sum of salaries department wise.

27. Write a SQL Query to fetch the Maximum salary department wise
SQL> select max(dept_sal) from employee_details2;

MAX(DEPT_SAL)
-------------
20000
28. Write a SQL Query to fetch the Minimum salary department wise.
SQL> select min(dept_sal) from employee_details2;

MIN(DEPT_SAL)
-------------
5000
29. Write an SQL query to fetch the employees belongs to ‘HR’ and ‘Sales’
department.
SQL> select dept_name from dept_details group by dept_name having
count(dept_name)>1;

DEPT_NAME
--------------------------------------------------------------------------------
hr
30. Write a SQL Query to fetch the count of employees, department wise.
SQL> select count(*) from employee_details2;
COUNT(*)
----------
5

31. Write a SQL Query to fetch the next three months based on current month.
SQL> select add_months('01-jan-2024',3)from dual;

ADD_MONTH
---------
01-APR-24
32. Write an SQL query to fetch the Initial Capital letter to the employee name
SQL> select initcap('shagufta') from employee_details2;

INITCAP(
--------
Shagufta
33. Write an SQL query to get the rank wise Salary to the employees based on Salary

SQL> select emp_name,dept_sal,rank() over(order by dept_sal desc) as rnk from


employee_details2;

EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL RNK
---------- ----------
ramesh
20000 1

suresh
10000 2

suresh
10000 2

EMP_NAME
--------------------------------------------------------------------------------
DEPT_SAL RNK
---------- ----------
shagufta
5000 4

xxx
123 5

34. Write an SQL query to get the data for employees belongs to ‘HR’ and ‘IT’
department
SQL> select count(*) from dept_details where dept_name = 'hr';

COUNT(*)
----------
2
35. Insert the data into Student table, in which for one of the student don’t
provide the marks.
Write a SQL query, if we don’t have the marks to a student then we should pass
marks as 35.

36. Provide few employee salaries with decimals and then write a SQL Statement to
do the round
off to one decimal.
SQL> select round(123.2345,2) from employee_details2;

ROUND(123.2345,2)
-----------------
123.23
37. Write a SQL Statement to get the number of characters in emp name from EMP
table.
SQL> select length('shagufta') from employee_details2;

LENGTH('SHAGUFTA')
------------------
8
38. Write a SQL Statement to get the concatenate the EMPNAME and LOCATION .
SQL> select concat(emp_name,locations) as fullname from employee_details2;

FULLNAME
--------------------------------------------------------------------------------
shaguftahyd
rameshhyd
sureshhyd
sureshhyd
xxxhyd

39. Write a SQL Statement to display only the first three characters from the EMP
name.
SQL> select substr('shagufta',1,3) from employee_details2;

SUB
---
sha

40. Write a SQL Statement to add 3 months to ’01-Jan-2023’. Statement should return
’01-Apr2023’ after adding the 3months to it.
SQL> select add_months('01-jan-2024',3)from dual;

ADD_MONTH
---------
01-APR-24

42. Write a SQL Statement to ignore the duplicate data from both STUDENT1 and
STUDENT2 and
display the remaining non-duplicate data.

SQL> select stud_no,name,marks from student1


2 union
3 select stud_no,name,marks from student2;

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
12
ramesh v
58
23
suresh v
88

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------

34
naresh d
76

56
suman b

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
45

64
dinesh r
98

76

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
vijay b
65

89
pranay v
89

7 rows selected.

SQL>

43. Write a SQL Statement to get all the data from both STUDENT1 and STUDENT2
including
duplicate data.

SQL> select stud_no,name,marks from student1


2 union all
3 select stud_no,name,marks from student2;

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
12
ramesh v
58

23
suresh v
88

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------

34
naresh d
76

56
suman b

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
45

64
dinesh r
98

76

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
vijay b
65

76
vijay b
65

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
89
pranay v
89

12
ramesh v
58

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------

23
suresh v
88

10 rows selected.

44. Write a SQL Statement to fetch the Student1 data matched records from STUDENT2
table.
SQL> select stud_no,name,marks from student1
2 intersect
3 select stud_no,name,marks from student2
4 ;

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
76
vijay b
65

12
ramesh v
58

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------

23
suresh v
88
45. Write a SQL Statement to fetch the Student1 data not matched records from
STUDENT2 table.

SQL> select stud_no,name,marks from student1


2 minus
3 select stud_no,name,marks from student2;

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
34
naresh d
76

56
suman b
45

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------

64
dinesh r
98

46. Write a SQL Statement to display all the students data from STUDENT2 table
except the stud_no
12 and 23.

SQL> select * from student2 where stud_no not in(12,23);

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------
76
vijay b
65

89
pranay v
89

STUD_NO
----------
NAME
--------------------------------------------------------------------------------
MARKS
----------

48. Write a SQL statement to fetch the stud_no, name, course_name details from
STUDENT_DETAILS and COURSES tables.

49. Write a SQL statement to fetch stud_no, name, course_name for all the student
details even
fetch the details if course is not assigned to a student.

50. Write a SQL statement to fetch stud_no, name, course_name for all the course
details, even if
any course is not assigned to a student also should fetch the details.

You might also like