0% found this document useful (0 votes)
2 views

DBMS - Reference for Placement

Uploaded by

madhanrajpun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS - Reference for Placement

Uploaded by

madhanrajpun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DBMS SQL QUERIES and Some of the questions for Reference

1. Create table student with required attributes which should get only mbbs and btech
as its degree.
create table student(rno int, name varchar(20), degree varchar(20), primary key(rno,name),
check( degree in(‘mbbs’,’btech’)));
2. Write a SQL query to Create a foreign key and primary key for a table
create table student(rno int primary key, name varchar(20), degree varchar(20))
create table studentacademic(rno int, name varchar(20), cgpa float, foreign key(rno)
references student);
//Here rno of studentacademic table act as foreign key and rno of student table act as
primary key
3. Display the names of students who have 4 characters
select * from student where name like ‘_ _ _ _ ‘ ;
4. Display the names of the students starting with p
select * from student where name like ‘p%’;
5. Display the names of the students whose name contains a characters ‘ya’
select * from student where name like ‘%ya%’;
6. To display the student id and student name of the student whose name has letters 'va'
from the table student
select studid, name from student where name like ‘%ev%’;
7. Display the total salary of the employee who belongs to cse department
//Consider a table employee contains attributes eno, ename, gender, department and salary
eno ename gender department salary
1 anbu male ece 25000
2 rajesh male cse 22000
3 abi female cse 40000
4 anand male ece 30000
5 keerthana female eee 18000

select sum(salary) from employee where department=‘cse’ ;


Note: Basic aggregate functions are: min, max, sum, avg, count
8. Display the count of number of employees in cse department
select count(*) from employee where department=‘cse’ ;
9. Display the maximum salary of employee, group by gender
select gender, max(salary) from employee group by gender ;
10. Display the maximum salary of employee, group by gender with maximum salary> 25000
select gender, max(salary) from employee group by gender having max(salary)>25000;
11. Display the maximum salary of employee, group by department with maximum salary>
25000
select department, max(salary) from employee group by department having max(salary)>25000;
12. Make the 4th employee salary as null
update employee set salary =null where eno=4;
13. Display the employee whose salary is null
select ename from employee where salary is null;
//It will display the 4th employee name anand
14. SQL query to change/update the data in third row, 2 nd column of the table
Update employee set ename=’priya’ where eno=3;
//We consider that the old ename in third row is ‘abi’ and the eno of 3rd row is 3

15. Count the number of characters for each employee name.


select length(ename) from employee;
16. Count the number of characters for each employee name without considering the
spaces in name.
select length(trim(ename)) from employee;
17. Write a query to display the employee name if cse department exists in employee
table
select ename from employee where department=’cse’;
18. Find the employee name whose salary is greater than atleast one(>=1) of the
employee’s salary in CSE department.
select ename from employee where salary > some( select salary from employee
where dept=‘cse’) ;
//Here first innermost query is executed. Then the outer query executed.
19. Find the employee name whose salary is greater than all the employee’s salary in
CSE department.
select ename from employee where salary > all( select salary from employee where
dept=‘cse’) ;
20. Create a report that displays the employee number, name, and salary of all
employees who earn more than the average salary. Sort the results in descemding
order by salary.
select eno,ename,salary from employee where salary > all( select avg(salary) from employee)
order by salary desc;
(or)
select eno,ename,salary from employee where salary > some( select avg(salary) from employee)
order by salary desc;
21. To display the college of the student from the table student by avoiding repeated
values.
select distinct college from student;
22. To display the records from the table student who belongs to VIT college.
select * from student where college=’VIT’;
23. To display the student id, student name and college of the student whose the semester
in between 2 and 4
sid name college Semester
1 ganesh srm 2
2 raja mit 3
3 hari mit 6
4 rajesh srm 4
select sid,name,college from student where semester between 2 and 4
//It will display sid 1,2,4 and corresponding name and college
24. To display the student id, student name of the student whose is not in srm college
select sid,name,college from student where college not in ‘srm’; (or)
select sid,name,college from student where college != ‘srm’;
25. SQL query to change/update the data in third row, 2 nd column of the table student
update student set name=’dhinesh’ where sid=3;
26. Find the nth largest salary of an employee.
SELECT * FROM employee emp1 WHERE(n-1)=SELECT
COUNT(emp2.Salary) FROM employee emp2 WHERE emp2.salary > emp1.salary);

Explanation:

 In the above query, emp1 and emp2 are the alias of the table employee.(i.e) both tables
emp1 and emp2 contains the same data which is in employee table
 First, all the salaries in table emp2 is compared with the first salary of emp1 table. Count is
incremented if it is greater. If that count matches with n-1, then the all the details in row 1
of emp1 is displayed.
 If count doesn’t match, all the salaries in table emp2 is compared with the second salary of
emp1 table. Count is incremented if it is greater. If that count matches with n-1, then the all
the details in row 2 of emp1 is displayed and so on.
Example:
 Consider I want to find the 4th largest salary of the employee:
emp2
Salary
25000 emp1
22000
40000 25000
30000
18000
 Here each salary in emp2 is compared with 1 st salary of emp1 table (i.e) 25000. When
comparing, two salaries 40000 and 30000 are greater than 25000. Hence the count is 2. But
it is not equal to 4-1.
emp2
Salary emp1
25000
22000
22000
40000
30000
18000
 Here each salary in emp2 is compared with 2nd salary of emp1 table(i.e) 22000. When
comparing, three salaries 25000, 40000 and 30000 are greater than 22000. Hence the count
is 3. It is equal to 4-1.
 Since it is equal, the value in emp1 is the 4th largest salary. Hence the entire details of the
employee getting salary 22000 is displayed.

27. Create a query that returns only two columns – called sid and name – ordered by
name in descending order.
select sid,name from student order by name desc;
28. You will need to concatenate sid and name to display them in one column.(Use concat
operation)
select concat(sid,name) as studdetails from student;
29. Create a table employee with required attributes such that salary of one of the
employee is made as null. Create a view for that employee table such that your view
should not contain that null value.
create view eview as select eno,ename,salary from employee where salary is not
null with check option;
30. Add one column cgpa to an existing table student.
Alter table student add cgpa float;
Fill the detail of cgpa to any of the student.
update student set cgpa =8.4 where sid=3
31. Difference between DBMS and RDBMS.
DBMS RDBMS
DBMS stores data as a file. RDBMS stores data in the form of tables. It
is the advanced version of DBMS
DBMS supports single user only. It supports multiple users
DBMS does not support the integrity RDBMS supports the integrity constraints
constants. at the schema level.
DBMS does not support Normalization RDBMS can be Normalized
DBMS system, stores data in either a RDBMS uses a tabular structure
navigational or hierarchical form
Examples of DBMS are a file system, XML, Example of RDBMS is MySQL, Oracle,
Windows Registry, etc SQL Server, etc.

32. Difference between Table and Relation

Table Relation
Table may contain duplicate values Relation must not contain duplicate values
Table is not necessarily a relation always Relation is a table always
In table, row order & column order are In relation, row and column order is not
mandatory considered
33. Different types of keys with examples.
Refer notes and internet.
34. Application of primary and foreign key.
There may be several foreign keys for a primary key.
Similar to the fact that, there may be several child for a parent in tree.
35. Joins concepts and its types
Joins can be applied on two tables only if there is a common attribute among those two
tables.
Types: self join, inner join(or) natural join, outer join(left,right,full) – See examples
Theta join: It is a join which can be performed even if a two tables doesn’t contain a
common attribute. For example:Refer
https://www.tutorialspoint.com/dbms/database_joins
36. Application of primary and foreign key.
There may be several foreign keys for a primary key.
Similar to the fact that, there may be several child for a parent in tree.
37. Difference between char and varchar2
char(n) : A fixed-length character string with user specified length
Example: An attribute is declared as char(20) and its value is ‘hello’ , remaining 15 spaces
are appended to make it 20 characters long.
varchar2(n) : A variable-length character string with user specified length
Example: An attribute is declared as varchar2(20) and its value is ‘hello’ , no spaces will be
added.Hence Memory is efficiently utilized in varchar2

38. Difference between NULL and empty in dbms


NULL Empty
Null is absence of value Empty string is a value. But it is empty
Null isn’t allocated any memory. A string Empty is allocated to memory location.
with null is just a pointer which is pointing Value stored in memory is ‘ ‘
to nowhere in memory

39. Difference between varchar and varchar2


Varchar Varchar2
varchar can identify NULL and empty Varchar2 cannot identify both separately.
string separately. Both considered as same for this.
Varchar can store minimum 1 and Varchar2 can store minimum 1 and
maximum 2000 bytes of character data. maximum 4000 bytes of character data.
Varchar is ANSI Sql standard Varchar2 is Oracle standard
Allocate fixed size of data irrespective of Allocate variable size of data based on
the input. input.

40. ACID property in DBMS?


Atomicity => Either all operation or no operation performed. (i.e) Operations are in-divisible
Consistency => Updation of data should be made in all the copies in database.
Example: Change of address/mobile number in saving account must also change in fixed
deposit account of that customer.
Isolation => Execution of one transaction will not affect the other.
Durability=> Updated value must persist even after the system crash or failure.
41. Transaction in DBMS => A single logical function/task. All transactions satisfy ACID
property
42. Normalization – Advantages and Disadvantages
Advantages: Normalization decomposes a relation into multiple relations, such that the
relation will be free from anomalies(insert/update/delete) and saves space by eliminating
redundancy of data
Disadvantages :Since Normalization decomposes a relation into multiple relations, we
need to use join to display the related records from multiple relations. This will increase the
overhead
43. Indexing in DBMS? Its importance?
Indexing is used to quickly locate and access the data in a database table
It optimize the performance of a database by minimizing the number of disk accesses
required when a query is processed.
44. Difference between Indexing and Hashing
Indexing: Requires traversing the index file to locate the record in a database table
Hashing: By using the hash function, the location of record is computed and it is accessed directly.
45. Examples for dbms.
Mysql, Oracle, RDBMS, IBM’s DB2, Microsoft SQL server, Microsoft access, HBASE,
Bigtable etc.
==>Know the latest versions of these dbms. Also know the differences
==> Know the dbms used by major companies like google, facebook, tcs, youtube,
linkedin etc.
46. Tools for bigdata?
Hadoop, mongodb, apache spark, Cassandra etc.
47. Database => Collections of tables. Tables can be organized as rows and columns.
Number of rows in table is called cardinality and Number of columns in table is called
degree
48. Constraints in a table?
1. Integrity constraints: Prevent invalid entries.
Example: If attribute declared as int only integer value can be entered,etc.
i) Unique => attribute value should not repeat
ii) Primary key => attribute value must not null and unique
2. Domain constraints:
not null =>Not null constraint specifies that the value of attribute should not be null.
check =>Check constraint specifies that the attribute values must satisfy specified condition.
3. Referential integrity constraints
49. When DDL and DML will be used?
i) DDL(Data Definition Language) => When operation is performed in structure of table,
DDL is used
DDL commands are: Create, Alter, Truncate, Drop, Rename
ii) DML(Data Manipulation Language) => When operation is performed in data inside
the table, DML is used
DML commands are: select, insert, update, delete
50. Applications of database system?
Banking, Reservation, Universities, Telecommunication, Sales, Social media, mailing,
searching, shopping websites, government, entertainment/video tutorials, hospitals etc.
51. Triggers, cursors – Its importance and examples
i)Trigger: When some modification done on a database (or) some abnormal events occur,
trigger need to be executed
Example: i) Invalid age(Age must be zero or greater than zero)
ii) Restrict insertion, deletion or updation
iii) Password condition in gmail/facebook/yahoo/instagram and in most of the websites
where signup or registration done
iv) Timeout condition in using a webpage(Ticket booking page, internet banking page etc)

ii) Cursors:
 It is a temporary work area used to store the data retrieved from database.
 It process each and every row in it.
 It Retrieves one row at a time, from a result set, unlike the SQL commands which
operate on all the rows in the result set at one time.
 Cursor is used, when the user needs to update records in a row by row manner, in a
database table
 sql%rowcount => Displays number of rows in resultset
 sql%found => returns true, if insert, update or delete operation affects 1 or more
rows
o Example: insert into employee values(5,’dhinesh’,’male’,’cse’,50000);
=> It will insert the new row to employee table. Hence sql%found returns
true
o All Updated rows are stored in cursor and it is processed.
o After this operation, if again sql%rowcount is executed, number of rows
will get changed.

You might also like