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

DB Ass 3 Solved

This document is an assignment submission for a database systems course. It was submitted by Mubashra Qadir with student ID MITE-F20-007 to instructor Abdul Rehman on July 26, 2021. The document pertains to assignment number 3 for the course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

DB Ass 3 Solved

This document is an assignment submission for a database systems course. It was submitted by Mubashra Qadir with student ID MITE-F20-007 to instructor Abdul Rehman on July 26, 2021. The document pertains to assignment number 3 for the course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Subject: Database Systems

Assignment: 3
Instructor: Abdul Rehman
Submitted by Mubashra Qadir
MITE-F20-007
26/07/2021

Department of Computer Science and Information Technology


Q31. Write a SQL query to find the names of employees that begin with ‘A’?
Ans: SQL query to find the names of employees that begin with ‘A’ is:
SELECT * FROM employees WHERE NAME LIKE ' A% ' ;

Q32. Write a SQL query to get the third highest salary of an employee from
employee_table?
Ans: Dense_rank function: Here we will use the dense_rank function, that computes the
rank of a row and returned the rank as number. This function accepts arguments in data type
only. In case of tie, it gives equal rank to all rows.
Select * from (select ename, sal, dense_rank()
Over (order by sal desc) r from Employee_table)
where r = &3;

Q33. What is the need for group functions in SQL?


Ans: Group functions produce results based on a group or set of rows. For example, with
group functions you can get count, sum, average, maximum, minimum, etc.
➢ COUNT - counts rows in a group
➢ SUM - totals the group
➢ AVG - averages the group
➢ MAX - finds maximum in the group
➢ MIN - find minimum in the group

Q34 . What is a Relationship and what are they?


Ans: A relationship in a database exists between two database tables when one table has a
foreign key that references the primary key of the other table. Relationships allow relational
databases to divide and store data in different tables, while connecting the data items. There
are three types of relationships that can exist between two entities. These are
➢ One-to-One Relationship.
➢ One-to-Many or Many-to-One Relationship.
➢ Many-to-Many Relationship.

Q35. How can you insert NULL values in a column while inserting the data?
Ans: You can insert NULL value into a column if the column must not have NOT NULL
constraints. Or whenever you do not pass any value for column compiler inserts null value by
default.
Q36. What is the main difference between ‘BETWEEN’ and ‘IN’ condition operators?
Ans: Both of these operators are used to find out the multiple values from the table.
BETWEEN operator is used to select a The IN operator allows you to specify
range of data between two values. multiple values.
Syntax: Syntax:
SELECT * FROM tbl_result SELECT * FROM tbl_result
WHERE marks BETWEEN 50 AND 80 WHERE marks IN (90, 95)

Q37. Why are SQL functions used?


Ans: SQL functions are simply sub-programs, which are commonly used throughout SQL
database applications for processing or manipulating data. The DML also supports functions,
and these are referred to as SQL functions. SQL functions are small programs that return only
one value.
Q38. What is the need of MERGE statement?
Ans: The MERGE statement is used to make changes in one table based on values matched
from anther. It can be used to combine insert, update, and delete operations into one
statement. A merge query creates a new query from two existing queries. One query result
contains all columns from a primary table, with one column serving as a single column
containing a relationship to a secondary table. The related table contains all rows that match
each row from a primary table based on a common column value.
Q39. What do you mean by recursive stored procedure?
Ans: A stored procedure can be recursive, referencing itself directly or indirectly. That is, the
stored procedure body can contain a CALL statement invoking the procedure being defined.
Such CALL statements can also be nested.
Q40. What is CLAUSE in SQL?
Ans: A clause in SQL is a part of a query that lets you filter or customizes how you want
your data to be queried to you.
➢ FROM clause
➢ GROUP BY clause
➢ HAVING clause
➢ WINDOW clause
➢ ORDER BY clause
Q41. What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’ CLAUSE?
Ans:
i.WHERE clause is used to specify a i.HAVING clause is used to specify a
condition for filtering records. condition for filtering values from a
group.
ii.The WHERE clause in MySQL is used ii.HAVING Clause cannot be used without
with SELECT, INSERT, UPDATE, and GROUP BY Clause
DELETE queries to filter data from the iii.HAVING Clause can only be used with
table or relation. SELECT statement.
iii.It describes a specific condition when
retrieving records from a single table or
multiple tables using the JOIN clause.
iv.The WHERE clause in MySQL can also
implement the logical connectives AND,
OR, and NOT.

Q42. List the ways in which Dynamic SQL can be executed?


Ans: Following are the ways in which dynamic SQL can be executed:
➢ Write a query with parameters.
➢ Using EXEC.
➢ Using sp_executesql.
Q43. What are the various levels of constraints?
Ans: SQL constraints are used to specify rules for data in a table. Constraints are used to
limit the type of data that can go into a table. This ensures the accuracy and reliability of the
data in the table. Constraints can be column level or table level.
Column level constraints apply to a Table level constraints apply to the whole
column. table.

Q44. How can you fetch common records from two tables?
Ans: You can use Intersect Key word, which gives you common records.
Syntax:
Suppose we have two tables student and student1, we will write the following query to find
the common records from the two tables:
(Select * from student) Intersect (Select * from student1)
Q45. List some case manipulation functions in SQL?
Ans: Some of case manipulation functions are:
1. LOWER
The lower function is used to convert a given String into lower case.
2. UPPER:
Upper Function converts given character String to Upper Case.
3. INITCAP
INITCAP Function converts every first letter of the word to uppercase and rest of them to
lower case in a given String.

Q46. What are the different set operators available in SQL?


Ans: Most oftenly used set operators in SQL are:
1. UNION: This clause combines the results of two SQL queries into a single table of all
matching rows. The two queries must result in the same number of columns and
compatible data types in order to unite. Any duplicate records are automatically removed
unless UNION ALL is used.
Syntax:
SELECT * FROM table1
UNION
SELECT * FROM table2;
2. INTERSECT: This operator takes the results of two queries and returns only rows that
appear in both result sets. For purposes of duplicate removal the INTERSECT operator
does not distinguish between NULLs.
Syntax:
SELECT * FROM table1
INTERSECT
SELECT * FROM table2;
3. EXCEPT: This operator takes the distinct rows of one query and returns the rows that do not
appear in a second result set. For purposes of row elimination and duplicate removal, the
EXCEPT operator does not distinguish between NULLs.
SELECT * FROM Orders
EXCEPT
SELECT * FROM Orders;
Q47. What is an ALIAS command?
Ans: SQL aliases are used to give a temporary name to a table or a column in a table.
Aliases are often used to make column names more readable. An alias only exists for the
duration of that query. An alias is created with the AS keyword.
Syntax:
SELECT column_name AS alias_name
FROM table_name;
Q48. What are aggregate and scalar functions?
Ans:
➢ Aggregate functions are the ➢ Scalar Functions are the calculations on
calculations on the set or group of the data given by the user, and they
value, and they return a single value. return a single value.
➢ .For example SUM, COUNT, AVG. ➢ For example UCASE, LCASE.

Q49. How can you fetch alternate records from a table?


Ans: To fetch even Numbered row:
SELECT * FROM table_name WHERE column_name % 2 = 0
To fetch odd Numbered row:
SELECT * FROM table_name WHERE column_name % 2 = 1
Q50. Name the operator which is used in the query for pattern matching?
Ans: LIKE is a keyword that is used in the pattern making.
Q51. How can you select unique records from a table?
Ans: SELECT DISTINCT returns only unique values. SELECT DISTINCT eliminates
duplicate values from the results.
Q52. How can you fetch first 5 characters of the string?
Ans: SELECT SUBSTRING (Column_Name, 1, 5) AS ExtractString
FROM table_name;
Q53. What is the main difference between SQL and PL/SQL?
Ans:
SQL is a standard Database language which PL/SQL stands for “Procedural language
is used to create, maintain and retrieve the extensions to SQL.” PL/SQL is a database-
relational database. oriented programming language that extends
SQL with procedural capabilities.

Q54. What is a View?


Ans: In SQL, a view is a virtual table based on the result-set of an SQL query. A view
contains rows and columns, just like a real table. The fields in a view are fields from one or
more real tables in the database.
Q55. What are Views used for?
Ans: Views are generally used to focus, simplify, and customize the perception each user has
of the database. Views can be used as security mechanisms by letting users access data
through the view, without granting the users permissions to directly access the underlying
base tables of the view.
Q56. What is a Stored Procedure?
Ans: A stored procedure is a set of Structured Query Language (SQL) statements with an
assigned name, which are stored in a relational database management system (RDBMS) as a
group, so it can be reused.
Q57. List some advantages and disadvantages of Stored Procedure?
Ans:
➢ It is faster. ➢ Testing of a logic which is
➢ It is pre-compiled. encapsulated inside a stored
procedure is very difficult.
➢ Need expert developer, since
difficult to write code

Q58. List all the types of user-defined functions?


Ans: 1. Scalar Function
2. Inline Table-Valued Function
3. Multi-Statement Table-Valued Function
Q59. What do you mean by Collation?
Ans: Collation is nothing but a set of rules that are predefined in SQL Server that determine
how the data in SQL Server are stored, retrieved and compared. By default in SQL Server the
collation is case insensitive
Q60. What are the different types of Collation Sensitivity?
Ans: Following are the different types of collation sensitivity:
1. Case Sensitivity: A and a and B and b.
2. Kana Sensitivity: Japanese Kana characters.
3. Width Sensitivity: Single byte character and double-byte character.
4. Accent Sensitivity.
Q61. What are Local and Global variables?
Ans: Local variable is declared inside a function whereas Global variable is declared outside
the function. Local variables are created when the function has started execution and is lost
when the function terminates, on the other hand, Global variable is created as execution starts
and is lost when the program ends.
Q62. What is Auto Increment in SQL?
Ans: Auto-increment allows a unique number to be generated automatically when a new
record is inserted into a table. Often this is the primary key field that we would like to be
created automatically every time a new record is inserted.
Q63. What is a Datawarehouse?
Ans: A data warehouse is a type of data management system that is designed to enable and
support business intelligence (BI) activities, especially analytics. Data warehouses are solely
intended to perform queries and analysis and often contain large amounts of historical data.
Q64. What are the different authentication modes in SQL Server? How can it be
changed?
Ans: There are two different authentication methods for connecting to SQL Server: Windows
and SQL Server. Here are times when you might want to change the authentication settings
for a SQL Server instance. One of those ways is to use SQL Server Management Studio
(SSMS). To use SSMS, first right click on the Instance name and select the Properties option.
The authentication options can be easily changed using the properties page in SSMS.
Q65. What are STUFF and REPLACE function?
Ans:
STUFF function is used to delete a REPLACE function name indicates, the
substring of a certain length of a string and replace function replaces all occurrences of
replace it with a new string. a specific string value with another string.

You might also like