SQL Interviews Flashcards

1
Q

What are the different subsets of SQL?

A

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.

Data Control Language(DCL) – It allows you to control access to the database. Example – Grant, Revoke access permissions

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

What is a Self-Join?

A

A self-join is a type of join that can be used to connect two tables. As a result, it is a unary relationship. Each row of the table is attached to itself and all other rows of the same table in a self-join. As a result, a self-join is mostly used to combine and compare rows from the same database table.

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

What is Cursor? How to use a Cursor?

A

After any variable declaration, DECLARE a cursor. A SELECT Statement must always be coupled with the cursor definition.

To start the result set, move the cursor over it. Before obtaining rows from the result set, the OPEN statement must be executed.

To retrieve and go to the next row in the result set, use the FETCH command.

To disable the cursor, use the CLOSE command.

Finally, use the DEALLOCATE command to remove the cursor definition and free up the resources connected with it.

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

What is OLTP?

A

OLTP, or online transactional processing, allows huge groups of people to execute massive amounts of database transactions in real time, usually via the internet. A database transaction occurs when data in a database is changed, inserted, deleted, or queried.

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

What are the differences between OLTP and OLAP?

A

OLTP stands for online transaction processing, whereas OLAP stands for online analytical processing. OLTP is an online database modification system, whereas OLAP is an online database query response system.

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

How to create empty tables with the same structure as another table?

A

To create empty tables:
Using the INTO operator to fetch the records of one table into a new table while setting a WHERE clause to false for all entries, it is possible to create empty tables with the same structure. As a result, SQL creates a new table with a duplicate structure to accept the fetched entries, but nothing is stored into the new table since the WHERE clause is active.

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

What is the usage of the NVL() function?

A

You may use the NVL function to replace null values with a default value. The function returns the value of the second parameter if the first parameter is null. If the first parameter is anything other than null, it is left alone.

This function is used in Oracle, not in SQL and MySQL. Instead of NVL() function, MySQL have IFNULL() and SQL Server have ISNULL() function.

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

What is the difference between the RANK() and DENSE_RANK() functions?

A

The RANK() function in the result set defines the rank of each row within your ordered partition. If both rows have the same rank, the next number in the ranking will be the previous rank plus a number of duplicates. If we have three records at rank 4, for example, the next level indicated is 7.

The DENSE_RANK() function assigns a distinct rank to each row within a partition based on the provided column value, with no gaps. It always indicates a ranking in order of precedence. This function will assign the same rank to the two rows if they have the same rank, with the next rank being the next consecutive number. If we have three records at rank 4, for example, the next level indicated is 5.

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

What are Tables and Fields?

A

A table is a collection of data components organized in rows and columns in a relational database. A table can also be thought of as a useful representation of relationships. The most basic form of data storage is the table. An example of an Employee table is shown below.

A Record or Row is a single entry in a table. In a table, a record represents a collection of connected data. The Employee table, for example, has four records.

A table is made up of numerous records (rows), each of which can be split down into smaller units called Fields(columns). ID, Name, Department, and Salary are the four fields in the Employee table above.

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

What is a UNIQUE constraint?

A

The UNIQUE Constraint prevents identical values in a column from appearing in two records. The UNIQUE constraint guarantees that every value in a column is unique.

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

What is the difference between CHAR and VARCHAR2 datatype in SQL?

A

Both Char and Varchar2 are used for characters datatype but varchar2 is used for character strings of variable length whereas Char is used for strings of fixed length. For example, char(10) can only store 10 characters and will not be able to store a string of any other length whereas varchar2(10) can store any length i.e 6,8,2 in this variable.

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

What is a Primary key?

A
  • A Primary key in SQL is a column (or collection of columns) or a set of columns that uniquely identifies each row in the table.
  • Uniquely identifies a single row in the table
  • Null values not allowed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are Constraints?

A

Constraints in SQL are used to specify the limit on the data type of the table. It can be specified while creating or altering the table statement. The sample of constraints are:

  • NOT NULL
  • CHECK
  • DEFAULT
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between DELETE and TRUNCATE statements?

A

Differences

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

What is a Unique key?

A
  • Uniquely identifies a single row in the table.
  • Multiple values allowed per table.
  • Null values allowed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a Foreign key in SQL?

A
  • Foreign key maintains referential integrity by enforcing a link between the data in two tables.
  • The foreign key in the child table references the primary key in the parent table.
  • The foreign key constraint prevents actions that would destroy links between the child and parent tables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What do you mean by data integrity?

A

Data Integrity defines the accuracy as well as the consistency of the data stored in a database. It also defines integrity constraints to enforce business rules on the data when it is entered into an application or a database.

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

What is the difference between clustered and non-clustered index in SQL?

A

The differences between the clustered and non clustered index in SQL are :

  1. Clustered index is used for easy retrieval of data from the database and its faster whereas reading from non clustered index is relatively slower.
  2. Clustered index alters the way records are stored in a database as it sorts out rows by the column which is set to be clustered index whereas in a non clustered index, it does not alter the way it was stored but it creates a separate object within a table which points back to the original table rows after searching.
  3. One table can only have one clustered index whereas it can have many non clustered index.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Write a SQL query to display the current date?

A

In SQL, there is a built-in function called GetDate() which helps to return the current timestamp/date.

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

What do you mean by Denormalization?

A

Denormalization refers to a technique which is used to access data from higher to lower forms of a database. It helps the database managers to increase the performance of the entire infrastructure as it introduces redundancy into a table. It adds the redundant data into a table by incorporating database queries that combine data from various tables into a single table.

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

What are Entities and Relationships?

A

Entities: A person, place, or thing in the real world about which data can be stored in a database. Tables store data that represents one type of entity. For example – A bank database has a customer table to store customer information. The customer table stores this information as a set of attributes (columns within the table) for each customer.

Relationships: Relation or links between entities that have something to do with each other. For example – The customer name is related to the customer account number and contact information, which might be in the same table. There can also be relationships between separate tables (for example, customer to accounts).

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

What is an Index?

A

An index refers to a performance tuning method of allowing faster retrieval of records from the table. An index creates an entry for each value and hence it will be faster to retrieve data.

23
Q

Explain different types of index in SQL

A

There are three types of index in SQL namely:

Unique Index:

This index does not allow the field to have duplicate values if the column is unique indexed. If a primary key is defined, a unique index can be applied automatically.

Clustered Index:

This index reorders the physical order of the table and searches based on the basis of key values. Each table can only have one clustered index.

Non-Clustered Index:

Non-Clustered Index does not alter the physical order of the table and maintains a logical order of the data. Each table can have many nonclustered indexes.

24
Q

What is Normalization and what are the advantages of it?

A

Normalization in SQL is the process of organizing data to avoid duplication and redundancy. Some of the advantages are:

  • Better Database organization
  • More Tables with smaller rows
  • Efficient data access
  • Greater Flexibility for Queries
  • Quickly find the information
  • Easier to implement Security
  • Allows easy modification
  • Reduction of redundant and duplicate data
  • More Compact Database
  • Ensure Consistent data after modification
25
Q

What is the difference between DROP and TRUNCATE commands?

A

DROP command removes a table and it cannot be rolled back from the database whereas TRUNCATE command removes all the rows from the table.

26
Q

Explain different types of Normalization

A

There are many successive levels of normalization. These are called normal forms. Each consecutive normal form depends on the previous one.The first three normal forms are usually adequate.

Normal Forms are used in database tables to remove or decrease duplication. The following are the many forms:

First Normal Form:
When every attribute in a relation is a single-valued attribute, it is said to be in first normal form. The first normal form is broken when a relation has a composite or multi-valued property.

Second Normal Form:

A relation is in second normal form if it meets the first normal form’s requirements and does not contain any partial dependencies. In 2NF, a relation has no partial dependence, which means it has no non-prime attribute that is dependent on any suitable subset of any table candidate key. Often, the problem may be solved by setting a single column Primary Key.

Third Normal Form:
If a relation meets the requirements for the second normal form and there is no transitive dependency, it is said to be in the third normal form.

27
Q

How many Aggregate functions are available in SQL?

A

SQL aggregate functions provide information about a database’s data. AVG, for example, returns the average of a database column’s values.

SQL provides seven (7) aggregate functions, which are given below:

AVG(): returns the average value from specified columns.
COUNT(): returns the number of table rows, including rows with null values.
MAX(): returns the largest value among the group.
MIN(): returns the smallest value among the group.
SUM(): returns the total summed values(non-null) of the specified column.
FIRST(): returns the first value of an expression.
LAST(): returns the last value of an expression.

28
Q

What is the default ordering of data using the ORDER BY clause? How could it be changed?

A

The ORDER BY clause in MySQL can be used without the ASC or DESC modifiers. The sort order is preset to ASC or ascending order when this attribute is absent from the ORDER BY clause.

29
Q

How do we use the DISTINCT statement? What is its use?

A

The SQL DISTINCT keyword is combined with the SELECT query to remove all duplicate records and return only unique records. There may be times when a table has several duplicate records.

The DISTINCT clause in SQL is used to eliminate duplicates from a SELECT statement’s result set.

30
Q

What are the syntax and use of the COALESCE function?

A

From a succession of expressions, the COALESCE function returns the first non-NULL value. The expressions are evaluated in the order that they are supplied, and the function’s result is the first non-null value. Only if all of the inputs are null does the COALESCE method return NULL.

The syntax of COALESCE function is COALESCE (exp1, exp2, …. expn)

31
Q

What is the ACID property in a database?

A

ACID stands for Atomicity, Consistency, Isolation, Durability. It is used to ensure that the data transactions are processed reliably in a database system.

Atomicity: Atomicity refers to the transactions that are completely done or failed where transaction refers to a single logical operation of a data. It means if one part of any transaction fails, the entire transaction fails and the database state is left unchanged.

Consistency: Consistency ensures that the data must meet all the validation rules. In simple words, you can say that your transaction never leaves the database without completing its state.
Isolation: The main goal of isolation is concurrency control.

Durability: Durability means that if a transaction has been committed, it will occur whatever may come in between such as power loss, crash or any sort of error.

32
Q

What do you mean by “Trigger” in SQL?

A

Trigger in SQL is are a special type of stored procedures that are defined to execute automatically in place or after data modifications. It allows you to execute a batch of code when an insert, update or any other query is executed against a specific table.

33
Q

Are NULL values same as that of zero or a blank space?

A

A NULL value is not at all same as that of zero or a blank space. NULL value represents a value which is unavailable, unknown, assigned or not applicable whereas a zero is a number and blank space is a character.

34
Q

What is the difference between cross join and natural join?

A

The cross join produces the cross product or Cartesian product of two tables whereas the natural join is based on all the columns having the same name and data types in both the tables.

35
Q

What is subquery in SQL?

A

A subquery is a query inside another query where a query is defined to retrieve data or information back from the database. In a subquery, the outer query is called as the main query whereas the inner query is called subquery. Subqueries are always executed first and the result of the subquery is passed on to the main query. It can be nested inside a SELECT, UPDATE or any other query. A subquery can also use any comparison operators such as >,< or =.

36
Q

What are the different types of a subquery?

A

There are two types of subquery namely, Correlated and Non-Correlated.

Correlated subquery: These are queries which select the data from a table referenced in the outer query. It is not considered as an independent query as it refers to another table and refers the column in a table.

Non-Correlated subquery: This query is an independent query where the output of subquery is substituted in the main query.

37
Q

What is the need for group functions in SQL?

A

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.

38
Q

How can you insert NULL values in a column while inserting the data?

A

NULL values in SQL can be inserted in the following ways:

  • Implicitly by omitting column from column list.
  • Explicitly by specifying NULL keyword in the VALUES clause
39
Q

What is the main difference between ‘BETWEEN’ and ‘IN’ condition operators?

A

BETWEEN operator is used to display rows based on a range of values in a row whereas the IN condition operator is used to check for values contained in a specific set of values.

40
Q

Why are SQL functions used?

A

SQL functions are used for the following purposes:

  • To perform some calculations on the data
  • To modify individual data items
  • To manipulate the output
  • To format dates and numbers
  • To convert the data types
41
Q

What is the need for MERGE statement?

A

This statement allows conditional update or insertion of data into a table. It performs an UPDATE if a row exists, or an INSERT if the row does not exist.

42
Q

What do you mean by recursive stored procedure?

A

Recursive stored procedure refers to a stored procedure which calls by itself until it reaches some boundary condition. This recursive function or procedure helps the programmers to use the same set of code n number of times.

43
Q

What is CLAUSE in SQL?

A

SQL clause helps to limit the result set by providing a condition to the query. A clause helps to filter the rows from the entire set of records.

For example – WHERE, HAVING clause.

44
Q

What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’ CLAUSE?

A

HAVING clause can be used only with SELECT statement. It is usually used in a GROUP BY clause and whenever GROUP BY is not used, HAVING behaves like a WHERE clause.

Having Clause is only used with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are a part of the GROUP BY function in a query.

45
Q

How can you fetch common records from two tables?

A

You can fetch common records from two tables using INTERSECT.

46
Q

Name the operator which is used in the query for pattern matching?

A

LIKE operator is used for pattern matching

47
Q

What is a View?

A

A view is a virtual table which consists of a subset of data contained in a table. Since views are not present, it takes less space to store. View can have data of one or more tables combined and it depends on the relationship.

48
Q

What are Views used for?

A

A view refers to a logical snapshot based on a table or another view. It is used for the following reasons:

  • Restricting access to data.
  • Making complex queries simple.
  • Ensuring data independence.
  • Providing different views of same data.
49
Q

What is a Stored Procedure?

A

A Stored Procedure is a function which consists of many SQL statements to access the database system. Several SQL statements are consolidated into a stored procedure and execute them whenever and wherever required which saves time and avoid writing code again and again.

50
Q

List some advantages and disadvantages of Stored Procedure?

A

Advantages:

A Stored Procedure can be used as a modular programming which means create once, store and call for several times whenever it is required. This supports faster execution. It also reduces network traffic and provides better security to the data.

Disadvantage:

The only disadvantage of Stored Procedure is that it can be executed only in the database and utilizes more memory in the database server.

51
Q

What do you mean by Collation?

A

Collation is defined as a set of rules that determine how data can be sorted as well as compared. Character data is sorted using the rules that define the correct character sequence along with options for specifying case-sensitivity, character width etc.

52
Q

What are the different types of Collation Sensitivity?

A

Following are the different types of collation sensitivity:

  • Case Sensitivity: A and a and B and b.
  • Kana Sensitivity: Japanese Kana characters.
  • Width Sensitivity: Single byte character and double-byte character.
  • Accent Sensitivity.
53
Q

What are Local and Global variables?

A

Local variables:

These variables can be used or exist only inside the function. These variables are not used or referred by any other function.

Global variables:

These variables are the variables which can be accessed throughout the program. Global variables cannot be created whenever that function is called.

54
Q

What is Auto Increment in SQL?

A

Autoincrement keyword allows the user to create a unique number to get generated whenever a new record is inserted into the table.
This keyword is usually required whenever PRIMARY KEY in SQL is used.

AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in SQL SERVER.