SQL Questions IV Flashcards

1
Q

Write down various types of relationships in SQL?

A

There are various relationships, namely:

One-to-One Relationship.
One to Many Relationships.
Many to One Relationship.
Self-Referencing Relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a trigger?

A

The trigger is a statement that a system executes automatically when there is any modification to the database.

In a trigger, we first specify when the trigger is to be executed and then the action to be performed when the trigger executes. Triggers are used to specify certain integrity constraints and referential constraints that cannot be specified using the constraint mechanism of SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Live Lock?

A

Livelock occurs when two or more processes continually repeat the same interaction in response to changes in the other processes without doing any useful work.

Livelock to sytuacja w systemach wielowątkowych, rozproszonych lub baz danych, w której operacje systemowe są aktywnie wykonywane, ale nie prowadzą do postępu w realizacji celu, czyli nie rozwiązują problemu, dla którego zostały zaprojektowane.

These processes are not in the waiting state, and they are running concurrently. This is different from a deadlock because in a deadlock all processes are in the waiting state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Case WHEN in SQL?

A

Control statements form an important part of most languages since they control the execution of other sets of statements. These are found in SQL too and should be exploited for uses such as query filtering and query optimization through careful selection of tuples that match our requirements. In this post, we explore the Case-Switch statement in SQL. The CASE statement is SQL’s way of handling if/then logic.

CASE case_value
WHEN when_value
THEN statement_list
[WHEN when_value THEN statement_list] …
[ELSE statement_list]
END CASE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name different types of case manipulation functions available in SQL.

A

LOWER: The purpose of this function is to return the string in lowercase. It takes a string as an argument and returns the string by converting it into lower case.

UPPER: The purpose of this function is to return the string in uppercase. It takes a string as an argument and returns the string by converting it into uppercase.

NITCAP: The purpose of this function is to return the string with the first letter in uppercase and the rest of the letters in lowercase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are local and global variables and their differences?

A

Global Variable: In contrast, global variables are variables that are defined outside of functions. These variables have global scope, so they can be used by any function without passing them to the function as parameters.

Local Variable: Local variables are variables that are defined within functions. They have local scope, which means that they can only be used within the functions that define them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name the function which is used to remove spaces at the end of a string?

A

In SQL, the spaces at the end of the string are removed by a trim function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define SQL Order by the statement?

A

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns.

By default ORDER BY sorts the data in ascending order.
We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain SQL Having statement?

A

HAVING is used to specify a condition for a group or an aggregate function used in the select statement. The WHERE clause selects before grouping. The HAVING clause selects rows after grouping. Unlike the HAVING clause, the WHERE clause cannot contain aggregate functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define BETWEEN statements in SQL?

A

The SQL BETWEEN condition allows you to easily test if an expression is within a range of values (inclusive). The values can be text, date, or numbers. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement. The SQL BETWEEN Condition will return the records where the expression is within the range of value1 and value2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

COMMIT

A

OMMIT permanently saves the changes made by the current transaction.

The transaction can not undo changes after COMMIT execution.

When the transaction is successful, COMMIT is applied.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ROLLBACK

A

ROLLBACK undo the changes made by the current transaction.

Transaction reaches its previous state after ROLLBACK.

When the transaction is aborted, ROLLBACK occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are ACID properties?

A

A transaction is a single logical unit of work that accesses and possibly modifies the contents of a database. Transactions access data using read-and-write operations. In order to maintain consistency in a database, before and after the transaction, certain properties are followed.

These are called ACID properties. ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that guarantee that database transactions are processed reliably. For more details please read ACID properties in the DBMS article.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ACID

A

Atomicity, Consistency, Isolation, Durability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Are NULL values the same as zero or a blank space?

A

In SQL, zero or blank space can be compared with another zero or blank space. whereas one null may not be equal to another null.

null means data might not be provided or there is no data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the need for group functions in SQL?

A

In database management, group functions, also known as aggregate functions, 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.

1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()

17
Q

What is the need for a MERGE statement?

A

The MERGE command in SQL is actually a combination of three SQL statements: INSERT, UPDATE, and DELETE. In simple words, the MERGE statement in SQL provides a convenient way to perform all these three operations together which can be very helpful when it comes to handling large running databases. But unlike INSERT, UPDATE, and DELETE statements MERGE statement requires a source table to perform these operations on the required table which is called a target table.

18
Q

What are the advantages of PL/SQL functions?

A

The advantages of PL / SQL functions are as follows:

We can make a single call to the database to run a block of statements. Thus, it improves the performance against running SQL multiple times. This will reduce the number of calls between the database and the application.

We can divide the overall work into small modules which becomes quite manageable, also enhancing the readability of the code.

It promotes reusability.

It is secure since the code stays inside the database, thus hiding internal database details from the application(user). The user only makes a call to the PL/SQL functions. Hence, security and data hiding is ensured.

19
Q

What is the SQL query to display the current date?

A

CURRENT_DATE returns to the current date. This function returns the same value if it is executed more than once in a single statement, which means that the value is fixed, even if there is a long delay between fetching rows in a cursor.

20
Q

What are Nested Triggers?

A

A trigger can also contain INSERT, UPDATE, and DELETE logic within itself, so when the trigger is fired because of data modification it can also cause another data modification, thereby firing another trigger. A trigger that contains data modification logic within itself is called a nested trigger.

21
Q

How to find the available constraint information in the table?

A

In SQL Server the data dictionary is a set of database tables used to store information about a database’s definition. One can use these data dictionaries to check the constraints on an already existing table and to change them(if possible)

22
Q

What is the difference between COALESCE() & ISNULL()?

A

COALESCE(): COALESCE function in SQL returns the first non-NULL expression among its arguments. If all the expressions evaluate to null, then the COALESCE function will return null.

ISNULL(): The ISNULL function has different uses in SQL Server and MySQL. In SQL Server, ISNULL() function is used to replace NULL values.