Concepts of SQL organized in a Q&A format

Image for post
  1. What is DBMS?

A Database Management System (DBMS) is a program that controls creation, maintenance and use of a database. DBMS can be termed as File Manager that manages data in a database rather than saving it in file systems.

2. What is RDBMS?

RDBMS stands for Relational Database Management System. RDBMS store the data into the collection of tables, which is related by common fields between the columns of the table. It also provides relational operators to manipulate the data stored into the tables.

3. What are tables and Fields?

A table is a set of data that are organized in a model with Columns and Rows. Columns can be categorized as vertical, and Rows are horizontal. A table has specified number of column called fields but can have any number of rows which is called record.

Example:.

Table: Employee.

Field: Emp ID, Emp Name, Date of Birth.

Data: 201456, David, 11/15/1960.

4. Difference between Primary Key and Unique Key?

Primary Key:

  • There can only be one primary key in a table
  • In some DBMS it cannot be NULL - e.g. MySQL adds NOT NULL
  • Primary Key is a unique key identifier of the record

Unique Key:

  • Can be more than one unique key in one table
  • Unique key can have NULL values
  • It can be a candidate key
Image for post

5. What is normalization?

Normalization is the process of minimizing redundancy and dependency by organizing fields and table of a database. The main aim of Normalization is to add, delete or modify field that can be made in a single table.

6.. What is an Index?

An index is performance tuning method of allowing faster retrieval of records from the table. An index creates an entry for each value and it will be faster to retrieve data. These indexes need extra space on disk, but they allow faster search according to different frequently searched values.

7 . What is the difference between DELETE and TRUNCATE commands?

DELETE command is used to remove rows from the table, and WHERE clause can be used for conditional set of parameters. Commit and Rollback can be performed after delete statement.

TRUNCATE removes all rows from the table. Truncate operation cannot be rolled back.

8. What is wrong with the below-given SQL query?

SELECT gender, AVG(age) FROM employee WHERE AVG(age)>30 GROUP BY gender

When we execute this command, we get the following error:

Msg 147, Level 16, State 1, Line 1

Aggregation may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, the column being aggregated is an outer reference.

Msg 147, Level 16, State 1, Line 1
Invalid column name ‘gender’.

This basically means that whenever we are working with aggregate functions and we are using GROUP BY, we can’t use the WHERE clause. Therefore, instead of the WHERE clause, we should use the HAVING clause.

Also, when we are using the HAVING clause, GROUP BY should come first and HAVING should come next.

select e_gender, avg(e_age) from employee group by e_gender having avg(e_age)>30

9. Main difference between HAVING and WHERE clause

The main difference between WHERE and HAVING clause comes when used together with GROUP BY clause, In that case WHERE is used to filter rows before grouping and HAVING is used to exclude records after grouping.

WHERE clause cannot contain aggregate function like COUNT(), SUM(), MAX(), MIN(), etc but HAVING clause may contain aggregate functions.

Another worth noting the difference between WHERE and HAVING clause is that WHERE is used to impose filtering criterion on a SELECT, UPDATE, DELETE statement as well as single row function and used before group by clause but HAVING is always used after group by clause.

10. What is the difference between BETWEEN and IN operators?

The BETWEEN operator is used to fetch rows based on a range of values.
For example,

SELECT * FROM Students 
WHERE ROLL_NO BETWEEN 20 AND 30;

This query will select all those rows from the table Students where the value of the field ROLL_NO lies between 20 and 30.

The IN operator is used to check for values contained in specific sets.
For example,

SELECT * FROM Students 
WHERE ROLL_NO IN (20,21,23);

This query will select all those rows from the table Students where the value of the field ROLL_NO is either 20 or 21 or 23.

11. Write an SQL query to find names of employee start with ‘A’

SELECT * FROM Employees WHERE EmpName like 'A%' ;

References

  1. https://www.guru99.com/sql-interview-questions-answers.html
  2. https://www.essentialsql.com/primary-and-unique-key/
  3. https://stackoverflow.com/questions/9565996/difference-between-primary-key-and-unique-key#:~:text=Both%20Primary%20key%20and%20Unique,does%20allow%20one%20NULL%20value%20.
  4. https://intellipaat.com/blog/interview-question/sql-interview-questions/
  5. https://www.java67.com/2019/06/difference-between-where-and-having-in-sql.html#:~:text=The%20main%20difference%20between%20WHERE,you%20write%20better%20SQL%20queries.
  6. https://www.geeksforgeeks.org/sql-interview-questions/

Written by

Product Manager and Data Analyst

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store