Day 2 PI Questions
Day 2 PI Questions
Day 2 PI Questions
Day: 2
Topics Covered: Set Operations (Union, Union all, Intersection, Cartesian product and set difference) and their
differences, Select command using different clauses and predicates like having, order by, group by, LIKE predicate,
In clause, between clause, AND, OR and NOT operations and Use of Flashback and timestamp command.SQL-What
is SQL?Aggregate functions,
Creating User
Creating Roles
Answer: CHECK constraints are rules used to limit values that are accepted by one or more columns.
For example, Products with unit price between INR 5000 to INR 100000 are allowed only. This prevents product unit
price from being entered beyond the regular price range.
The logical expression would be the following: UnitPrice>= 5000 AND UnitPrice<= 100000
Question 2: Why do we need to have columns that are not part of GROUP BY Clause as part of aggregate function
Answer:- This is also the same reason as above. If we have some columns in the SELECT list that are not part of the
GROUP BY clause, then it will change the requirement of the query itself. Still if we need to include them in
SELECT list but not in GROUP BY clause, then we have use some aggregate function on them so that their value
remains same but it will be displayed in the SELECT lis
Answer:- To safeguard enterprise data (to make enterprise data consistent and to achieve data integrity).A
transaction is the propagation of one or more changes to the database. For example, if we are creating a record or
updating a record or deleting a record from the table, then we are performing a transaction on the table. It is important
to control transactions to ensure data integrity and to handle database errors.
Answer:- The rule of transaction tells that either all the statements in the transaction should be executed successfully
or none of those statements to be executed.
Answer: The particular statement is used with the SELECT statement. In the event that the tables contain duplicate
values then DISTINCT is used to choose diverse values among duplicate tables.
E.g.SELECT DISTINCT Country FROM Customers;
Answer:Group functions work on the set of rows and return one result per group. Some of the commonly used group
functions are: AVG, COUNT, MAX, MIN, SUM, VARIANCE.
Answer: Yes, you can have nested aggregate functions up to two levels deep. For example, you can use
MAX(COUNT(*)).
Question 10. What’s the difference between COUNT(column) and COUNT(DISTINCT column)?
Answer: COUNT(column) will return the number of non-NULL values in that column. COUNT(DISTINCT
column) will return the number of unique non-NULL values in that column.
Answer: There is no GROUP BY clause and it will display an error. Because we have used the COUNT function,
which is an aggregate function, along with a database field, we need to add a GROUP BY clause. It should GROUP
BY the department_id column.
FROM department
GROUP BY department_id;
Answer: The WHERE clause cannot include any checks on the aggregate column – even if a GROUP BY has been
performed.This is because the WHERE happens before the grouping, so there is no way for the WHERE clause to
know what the value of the COUNT function is.To resolve this, use the HAVING clause to check for COUNT(*) > 5.
Question 14. What are the different DDL commands in SQL? Give a description of their purpose.
Answer:- Data Definition Language (DDL) – It allows you to perform various operations on the database such as
CREATE, ALTER, and DELETE objects.
Data Manipulation Language(DML) – It allows you to access and manipulate data. It helps you to insert, update,
delete and retrieve data from the database.
Department of Computer Science and EngineeringPage 3
Data Control Language(DCL) – It allows you to control access to the database. Example – Grant, Revoke access
permissions.
Answer: COMMIT statement makes all the updations made by all commands to be stored permanently whereas
ROLLBACK erases all the updations made by commands.
Answer: Check point is a mechanism that aids in database recovery. Checkpoint declares a point before which the
DBMS was in consistent state and all the transactions have been committed.
Answer: A relational database management system (RDBMS) is a set of applications and features that allow IT
professionals and others to develop, edit, administer, and interact with relational databases. Most commercial
relational database management systems use Structured Query Language (SQL) to access the database, which is
stored in the form of tables.
The RDBMS is the most widely used database system in businesses all over the world. It offers a stable means of
storing and retrieving massive amounts of data..
Answer: Aggregate functions are used to evaluate mathematical calculation and return single values. This can be
calculated from the columns in a table. Scalar functions return a single value based on the input value.
Example -.
Answer: SQL refers to the Standard Query Language, which is not actually the programming language. SQL doesn't
have a loop, Conditional statement, logical operations, it can not be used for anything other than data manipulation. It
is used like commanding (Query) language to access databases. The primary purpose of SQL is to retrieve,
manipulate, update and perform complex operations like joins on the data present in the database.
Answer: The UNION operator combines and returns the result-set retrieved by two or more SELECT statements.
The MINUS operator in SQL is used to remove duplicates from the result-set obtained by the second SELECT query
from the result-set obtained by the first SELECT query and then return the filtered results from the first.
The INTERSECT clause in SQL combines the result-set fetched by the two SELECT statements where records from
one match the other and then returns this intersection of result-sets
Answer: In SQL, the GRANT statement is used to give (or “grant”) one or more privileges/permissions to a
particular database user. Here’s what the syntax for the GRANT statement looks like:
GRANT privilege [, privilege ... ] /* can add more privileges here too */
Answer: It should be clear from the syntax of the GRANT statement shown above that you can specify more than
one database user or role that should receive the privilege(s) being granted.
Answer: In SQL, the REVOKE statement is used to take back/remove (or “revoke”) one or more
privileges/permissions from a particular database user. Here’s what the syntax for the REVOKE statement looks like:
Answer: It should be clear from the syntax of the REVOKE statement shown above that you can specify more than
one database user or role that should receive the privilege(s) being revoked. In this context, GRANTEE’s refer to the
users from whom the privileges are being revoked.
Answer: When a COMMIT is used in a transaction all changes made in the transaction are written into the database
permanently.
BEGIN TRANSACTION;
COMMIT TRANSACTION;
Answer: Rights are given to a user, usually by the DBA, to perform a particular action on the database schema
objects like creating tablespaces.
The following are examples of system privileges that can be granted to users:
Answer: An object-level privilege is a permission granted to a database user account or role to perform some action
on a database object. These object privileges include SELECT, INSERT, UPDATE, DELETE, ALTER, INDEX on
tables, and so on.
The following examples are object privileges that can be granted to users:
Question 32: What is the role of GRANT and REVOKE commands with example?
Answer: The GRANT command enables privileges on the database objects and the REVOKE command removes
them. They are DCL commands:-
Answer: A transaction can be defined as the sequence task that is performed on databases in a logical manner to
gain certain results. Operations like Creating, updating, deleting records performed in the database come from
transactions.
In simple words, we can say that a transaction means a group of SQL queries executed on database records.
ROLLBACK: It is used to roll back the transaction. All changes made by the transaction are reverted back and the
database remains as before.
SAVEPOINT: It is used to set the point where the transaction is to be rolled back.
Answer: SQL GRANT and REVOKE commands are used to implement privileges in SQL multiple user
environments. The administrator of the database can grant or revoke privileges to or from users of database objects
by using commands like SELECT, INSERT, UPDATE, DELETE, ALL, etc.
GRANT Command: This command is used to provide database access to users other than the administrator.
Syntax:
GRANT privilege_name
ON object_name
GRANT privilege_name
ON object_name
TO {user_name|PUBLIC|
role_name}
In the above syntax, the GRANT option indicates that the user can grant access to another user too.
REVOKE command: This command is used to provide database deny or remove access to database objects.
Syntax:
REVOKE privilege_name
ON object_name
FROM {user_name|PUBLIC|
role_name};