SQL - interview questions Flashcards

1
Q

Q: What are the Constraints?

A
A: Constraints 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
2
Q

Q: What do you mean by data integrity?

A

A: Data Integrity defines 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
3
Q

Q: What is an Index?

A

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.

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

Q: Explain the different types of Normalization.

A

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.

  • First Normal Form (1NF) – No repeating groups within rows
  • Second Normal Form (2NF) – Every non-key (supporting) column value is dependent on the whole primary key.
  • Third Normal Form (3NF) – Dependent solely on the primary key and no other non-key (supporting) column value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: What is ACID property in a database?

A

A: ACID stands for Atomicity, Consistency, Isolation, and 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.

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

Q: Write an SQL query to get the third-highest salary of an employee from employee_table?

A

A: SELECT TOP 1 salary FROM( SELECT TOP 3 salary FROM employee_table ORDER BY salary DESC) AS emp ORDER BY salary ASC;

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

Q: Write an SQL query to find the names of employees start with ‘A’?

A

A: The LIKE operator of SQL is used for this purpose. It is used to fetch filtered data by searching for a particular pattern in where clause. The Syntax for using LIKE is,

SELECT column1,column2 FROM table_name WHERE column_name LIKE pattern;

LIKE: operator name
pattern: exact value extracted from the pattern to get related data in result set.

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

Q: What do you mean by data definition language?

A

A: Data definition language or DDL allows you to execute queries like CREATE, DROP and ALTER. That is those queries which define the data.

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

Q: What is the difference between primary key and unique constraints?

A

A: Primary key cannot have a NULL value, the unique constraints can have NULL values. There is only one primary key in a table, but there can be multiple uniques constraints. The primary key creates the clustered index automatically but the unique key does not.

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

Q: What do you mean by foreign key?

A

A: A Foreign key is a field that can uniquely identify each row in another table. And this constraint is used to specify a field as a foreign key. That is this field points to the primary key of another table. This usually creates a kind of link between the two tables.

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

Q: What is the difference between DELETE and TRUNCATE statements?

A

DELETE: Delete command is used to delete a row in a table.

You can rollback data after using the delete statement. It is slower than the truncate statement

Truncate: Truncate is used to delete all the rows from a table. It is faster. Then delete

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

Q: What is a Relationship and what are they?

A
A: Relation or links are between entities that have something to do with each other. Relationships are defined as the connection between the tables in a database. There are various relationships, namely:
•	One to One Relationship.
•	One to Many Relationship.
•	Many to One Relationship.
•	Self-Referencing Relationship.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

A: NULL values can be inserted in the following ways:
• Implicitly by omitting column from column list.
• Explicitly by specifying NULL keyword in the VALUES clause

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

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

A

A: HAVING clause can be used only with the 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 used only with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are part of the GROUP BY function in a query.

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

Q: How can you fetch the first 5 characters of the string?

A

A: There are a lot of ways to fetch characters from a string. For example:

Select SUBSTRING(StudentName,1,5) as studentname from student

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