Questions (SQL) : Saved To The Database. DCL
Questions (SQL) : Saved To The Database. DCL
Insert Anomaly
If a new worker joins the firm and is currently unassigned to any department, we will be unable
to put the data into the table because the w_dept field does not allow nulls.
Delete Anomaly
If the corporation closes the department F890 at some point in the future, deleting the rows
with w_dept as F890 will also erase the information of employee Mike, who is solely assigned to
this department.
23. What are constraints in Sql? Not Null , Primary , Unique ,Foreign , check, Default , Index
Primary key : its uniquely identifies each record in a column as unique and non-repetitive.
Foreign key : Used to remove anomalies by setting relationship between tables. It is used to link
two tables and also called as referencing key. There is a parent child relation ship
Not Null : It enforce our column not to accept null values means field should always contain
some values.
Unique : It ensures that all columns are unique.
Check : It is used to put conditions on field.
Index : it is used to index in table, used to increase speed of accessing data. User cannot see it.
Only used on columns that are searched more often.
24. What are constraints ?
Constraints are used to impose rules and regulation in table and column level.
25. What are different keys in Sql ? Primary , unique , Foreign , Composite
26. Difference between unique and primary key?
27. Difference between cluster and non cluster index ?
Cluster index physically stores data of table in order of key values and data is restored everytime
whenever a new value is inserted or updated. No separate storage required. It is always index id
as 1. Primary key creates cluster index by default
Non cluster index create separate list of key values that points towards location of the data in
datapage. It stores data separately. Non cluster index id is less than 1.unique key creates non
cluster index by default.
Questions (SQL)
28. Is semicolon used after sql? Justify why or why not.
29. What are all the aggregate functions? Always used with select and group by (if using
multiple columns) Max, min, count, Sum, Avg
30. Why do we use where clause?
31. Difference between where and having?
Both WHERE and HAVING are used to filter data in an SQL query. The main difference is that
WHERE is used on non-aggregated values, while HAVING is used on the aggregated ones. The
order of execution is as follows: WHERE – GROUP BY – HAVING. This also means WHERE is
written before GROUP BY, while HAVING comes after GROUP BY.
In practice, WHERE filters data first. This filtered data will be grouped and aggregated, and then
HAVING will filter the grouped and aggregated data
32. Difference between group by and order by?
33. List select, aggregate , group by, having etc in order of execution?
34. How do you filter groups in an SQL query? Having
35. What is the difference between COUNT(*), COUNT(expression), and COUNT(DISTINCT
expression)?
36. What happens to NULLs when you use GROUP BY?
The GROUP BY clause doesn’t ignore NULL values. Therefore, if you use it and there are NULL
values in the column, all the rows with NULL column values will be treated as one group
37. How to Count SQL NULL values in a column? The COUNT() function is used to obtain the
total number of the rows in the result set. When we use this function with the star sign it
count all rows from the table regardless of NULL values
38. AVG() function and SQL NULL values? One point to note about the AVG() function
calculation is that NULL values will not be included in the average calculation.
39. What is the difference between ‘IS NULL’ and ‘= NULL’?
40. Use of count with null values?
COUNT():
Using the COUNT() syntax counts all rows in a table, including those with null values in the
specified column(s). Null values are included in the count. For example, if you have a table with
10 rows, and one of the columns contains 3 null values, COUNT(*) will return 13 as the result.
COUNT(column):
When you use the COUNT(column) syntax, the function counts the number of non-null values in
the specified column. It excludes null values from the count. For example, if you have a table
with 10 rows and one of the columns contains 3 null values, COUNT(column) will return 7 as the
result.
COUNT(DISTINCT column):
The COUNT(DISTINCT column) syntax counts the number of distinct non-null values in the
specified column. It also excludes null values from the count. Duplicate non-null values are only
counted once. Null values are not considered for distinct counting. For example, if you have a
table with 10 rows, one of the columns contains 3 null values, and the non-null values in that
column are [A, B, C, C, D, E, E], COUNT(DISTINCT column) will return 5 as the result.
Questions (SQL)
41. What are views in columns?
In SQL, a view is a virtual table based on the result-set of an SQL statement.
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.
You can add SQL statements and functions to a view and present the data as if the data were
coming from one single table.
A view is created with the CREATE VIEW statement.
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
42. Can you give me some examples of why we would want to use views?
Views can be used for a number of reasons. For example, they can be used to restrict access to
certain data so that only authorized users can see it. Views can also be used to simplify complex queries
by breaking them down into smaller, more manageable pieces. Additionally, views can be used to
provide an alternative way of looking at the data in a database, which can be useful for different types of
users.
Wildcard characters are used with the LIKE operator. The LIKE operator is
used in a WHERE clause to search for a specified pattern in a column.
Wildcards in Ms access and sql server –
[] Represents any single character within h[oa]t finds hot and hat,
the brackets but not hit
^ Represents any character not in the h[^oa]t finds hit, but not
brackets hot and hat
- Represents any single character within c[a-b]t finds cat and cbt
the specified range
Atomicity
The transaction never remains in between either it performs completely or gets discarded
completely in the case of any system or DBMS failure. Read more about Atomicity in DBMS.
Consistency
Data is very precious and it should remain the same before and after the transaction. It shouldn’t
show any inconsistent behaviors with DBMS
Questions (SQL)
Isolation
All transactions should be isolated from one another, which means every transaction should be
separate from one another there shouldn’t be any interference of one transaction onto another.
Durability
After execution of the entire transaction, data should become permanent on the database. It
should be durable i.e. it should remain there in case of system failure or system crash.
DATABASE :In simple words, a database is the collection of data that is organized in such
a way, so that it becomes easy to retrieve, update and delete that data.
DBMS : DBMS stands for Database Management System; it is the software used to
work or manage the databases. The main work of DBMS is to provide an interface for
the user to manipulate data from the database.
DBMS acts as a middle layer between the user and the actual database.
Types of DBMS:
1. Centralised
2. Decentralised
3. Network
4. Hierarchial
5. NoSql - NoSQL or non-relational databases are the recently popular databases due to their high
scalability and availability. In this type of database, the data is stored in collections and it doesn’t
contain tables like relational databases.
6. RDBMS -The relational database management system is also known as RDBMS. It is one of the
types of DBMS which is widely used for commercial applications. It contains tables in which data
is stored in the form of rows and columns like an excel sheet. Some of the tables possess the
relationship among them and the data is retrieved with the help of join operation.
Questions (SQL)
Flexibility Structured data is less flexible and There is an absence of schema, so it is more
schema-dependent. flexible.
Performance Here, we can perform a structured While in unstructured data, textual queries are
query that allows complex joining, possible, the performance is lower than semi-
so the performance is higher. structured and structured data.
Subqueries that return a single row as an output to their parent query are called single-row subqueries.
Single-row subqueries are used in a SQL SELECT statement with HAVING clause, WHERE clause, or a
FROM clause and a comparison operator. Single-row subqueries are used in the SELECT statement. Let's
see it with an example.
Subqueries that return multiple rows as an output to their parent query are called multiple-row
subqueries. Multiple row subqueries can be used in a SQL SELECT statement with a HAVING clause,
WHERE clause, a FROM clause, and a logical operator(ALL, IN, NOT IN, and ANY).
Correlated Subqueries
Subqueries that return multiple columns as output depending on the information obtained from the
parent query are called correlated subqueries. The interdependence of the inner and outer query makes
it complicated to understand.
Nested Subqueries
Subqueries that are inside another subquery are called nested subqueries. Subqueries are executed level
by level. The innermost is executed first, and then the outer ones.
Answer: D. The sub-query always executes before the execution of the main query.Subqueries
are completed first.The result of the subquery is used as input for the outer query.
4.Which of the following clause is mandatorily used in a sub-query?
A. SELECT
Questions (SQL)
B. WHERE
C. ORDER BY
D. GROUP BY
Answer: A. A sub-query is just like any other query which has to start with a SELECT clause.
They are contained within an outer query.
7.In which of the following clauses can a sub-query be used?
A. HAVING
B. WHERE
C. FROM
D. All of the above
8.Which of the following single-row operators can be used for writing a sub-query?
A. >=
B. <
C. =
D. All of the above
Answer: D. Single-row operators include =, >, <, >=, <=, and <>
12.What among the following is true about sub-queries?
Answer: A. Sub queries can be placed on left or right hand side of the comparison operator
depending on the query indentation and usability.