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

Q.Bank Solution DBMS

Uploaded by

saqibzuber813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Q.Bank Solution DBMS

Uploaded by

saqibzuber813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DBMS Question Bank Answer P.

A Test -2

Q.1) Describe views and write command to create views.


Ans:- Describe Views and write a command to create view. A view is a virtual table based on the result set of
the SQL statement. The fields in a view are fields from one or more than one table in the database. SQL
functions, where, join statements can be added to a view and the data in it can be presented as if it were
from one table. The database engine recreates the data, using the view’s SQL statement, every time a user
queries a view. A view can be updated using the create or replace view command. For deleting a view, drop
query can be used.

General syntax to create a view: create view view_name as select query.

Eg: create view view_student as select stud_id, stud_name,ssc_per from student;

2. Explain any four string functions with example.

Ans:- Initcap(String) – converts first character of string to upper case

Upper(String) – converts the string to upper case

Lower(String) – converts string to lower case

Length(String) – returns the number of characters in the string

Instr(String, sub) – returns the location of the substring

Lpad(String,char,number) – returns the string left padded with the character specified to a total of length
specified.

Rpad(String,char,number) – returns the string right padded with the character specified to a total of length
specified.

Ltrim(String) -removes white space or other specified characters from the left end of the string Rtrim(String)-
-removes white space or other specified characters from the right end of the string

Replace(String, char,char) – replace all occurrence of a substring by another substring

Substring(String,number) – extracts substring from the string

Translate(String,char,char) – replace all occurrence of characters by other characters

3. Explain types of aggregate function.

Ans:- An aggregate function is a function where the values of multiple rows are grouped together as input
on certain criteria to form a single value of more significant meaning.

Aggregate functions are :

1) Count()

2) Sum()

3) Avg()

4) Min()
DBMS Question Bank Answer P.A Test -2

5) Max()

1. Count () - 1) It returns number of rows from the given table if no attribute is mentioned.

2) If some attribute is mentioned, it gives total number of not null values for that attribute.

Eg :Select count(*) from emp; Returns total number of records from emp table.

1) Select count(telephone) from emp; Returns total number of employees having telephone numbers.

2. Sum() - It give total of all values from a numeric attribute of the given table,

Eg :Select sum(salary) from emp; Returns total salary drawn of all employees from the emp table.

3. Avg () - It gives average of all the numeric values of the given attribute from the table.

Eg :Select Avg(salary) from emp; Returns average salary of employees from emp table.

4. Min () - It gives minimum of all the values of the numeric given attribute from the table.

Eg :Select Min(salary) from emp; Returns minimum salary value from emp table,

5. Max () - It gives maximum of all the values of the numeric given attribute from the table. Eg :Select
Max(salary) from emp; retunes maximum salary value from emp table,

4. Explain GROUP BY, HAVING, ORDER BY clause with example.

Ans:- The GROUP BY clause is applicable when we want to use aggregate functions to more than one set of
rows. The ORDER BY clause is applicable when we want to get the data obtained by a query in the sorting
order. The HAVING clause was introduced in SQL to allow the filtering of query results based on aggregate
functions and groupings.
The following is the syntax to use GROUP BY clause in a SQL statement:

GROUP BY clause Example:

Student_Dept
Sid Sname Dept
1 Abc CS
2 Xyz EE
3 Pqr EE
4 Lmn CE
5 Uvw CE
DBMS Question Bank Answer P.A Test -2

The following is the Syntax to use the ORDER BY clause in a SQL statement:

ORDER BY Clause Example:

SELECT country, COUNT(*) AS num_customers

FROM customers

GROUP BY country

ORDER BY num_customers DESC;

HAVING clause

The HAVING clause is used to filter the results of a GROUP BY query. It allows you to specify conditions
that the groups must meet in order to be included in the results.

Example:

SELECT country, COUNT(*) AS num_customers

FROM customers

GROUP BY country

HAVING COUNT(*) > 100;

5. Explain all types of join with example and suitable

diagram.

Ans:- SQL Join statement is used to combine data or rows from two or more tables based on a common
field between them. Different types of Joins are as follows:

1) INNER JOIN or EQUI JOIN: A join which is based on equalities is called equi join. In equi join
comparison operator “=” is used to perform a Join.

Syntax: SELECT tablename.column1_name,tablename.column1_name FROM table_name1,table_name2


where table_name1.column_name=table_name2.column_name; Example: Select stud_info.stud_name,
stud_info.branch_code, branch_details.location From stud_info, branch_details Where
Stud_info.branch_code=branch_details.branch_code;

2) SELF JOIN: The SQL SELF JOIN is used to join a table to itself, as if the table were two tables,
temporarily renaming at least one table in the SQL statement. Syntax: SELECT a.column_name,
b.column_name FROM table1 a, table1 b WHERE a.common_filed = b.common_field; Example: Select
x.stud_name, y.stud_name from stud_info x, stud_info y Where x.leader= y.stud_id;
DBMS Question Bank Answer P.A Test -2

3) LEFT OUTER JOIN: A left outer join retains all of the rows of the “left” table, regardless of whether
there is a row that matches on the “right” table. Syntax: Select column1name,column2name from
table1name any_alias1 ,table2name any_alias2 on any_alias1.columnname(+) = any_alias2.columnname;
OR

Select column1name,column2name from table1name left outer join table2name on


table1name.columnname= table2name.columnname; Example: select last_name, department_name
from employees e, departments d on e.department_id(+) = d.department_id; OR select last_name,
department_name from employees left outer join departments on employees.department_id =
departments.department_id;

4) RIGHT OUTER JOIN: A right outer join retains all of the rows of the “right” table, regardless of whether
there is a row that matches on the “left” table. Syntax: Select column1name, column2name from
table1name any_alias1, table2name any_alias2 on any_alias1.columnname =any_alias2.columnname (+);
OR Select column1name, column2name from table1name any_alias1 right outer join table2 name
any_alias2 on any_alias1.columnname =any_alias2.columnname; Example: Select
last_name,department_name from employees e, departments d on e.department_id =
d.department_id(+); OR Select last_name, department_name from employees e right outer join
departments d on e.department_id = d.department_id;

5) NON EQUI JOIN: Non equi joins is used to return result from two or more tables where exact join is not
possible. Syntax: Select aliasname.column1name, aliasname.column2name from tablename alias where ;
For example: In emp table and salgrade table. The salgrade table contains grade and their low salary and
high salary. Suppose you want to find the grade of employees based on their salaries then you can use
NON EQUI join. Select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between
s.lowsal and s.hisal;

6. Create sequence for student table.

Ans:-

CREATE SEQUENCE student_id_seq

START WITH 1

INCREMENT BY 1

MINVALUE 1

MAXVALUE 99999

CYCLE;

7. Define index and Explain it's types.

Ans:- An index in a database is a data structure that helps the database find data quickly. There are two
main types of indexes: primary and secondary. Indexes can improve query performance, but they also add
overhead.

Primary: Created on the primary key, unique for each row.

Secondary: Created on non-key columns.


DBMS Question Bank Answer P.A Test -2

Clustered: Stores data in the same order as the index.

Non-clustered: Stores data in a separate structure from the table.

Dense: Contains an entry for every value in the indexed column.

Sparse: Only contains an entry for some of the values in the indexed column.

Multilevel: Organized into multiple levels, used for large tables.

8. What is sequence? Why it is used?

Ans:- A sequence in a database management system (DBMS) is a schema object that generates a series
of unique numeric values. Sequences are often used to generate primary key
values for tables, but they can also be used to generate other types of unique
values, such as order numbers, invoice numbers, and audit trail identifiers.

Uses:

Generating unique values for primary keys and other columns.

Generating sequential values for columns such as order numbers and invoice numbers.

Generating values for columns that are used in auditing or logging.

Improving the performance of queries.

9. write advantages and disadvantages of views.

Ans:-

Advantages of views:

Simplify data for users

Restrict access to data

Enforce data integrity

Improve query performance

Disadvantages of views:

Complex to create and maintain

Can degrade query performance

Dependent on underlying tables

10. Explain any four date and time function with Example.
DBMS Question Bank Answer P.A Test -2

Ans:- Four SQL date and time functions with examples:

NOW() returns the current date and time.

DATE_FORMAT(date_time_value, format) formats a date and time value according to a specified format.

DATE_ADD(date_time_value, interval) adds a specified interval to a date and time value.

DATE_SUB(date_time_value, interval) subtracts a specified interval from a date and time value.

You might also like