General SQL Interview Questions
General SQL Interview Questions
1. What is a Database?
A database is a collection of information in an organized form for
faster and better access, storage and manipulation. It can also be
defined as a collection of tables, schema, views and other
database objects.
8. What is DBMS?
Database Management System is a collection of programs that
enables a user to store, retrieve, update and delete information
from a database.
9. What is RDBMS?
RDBMS stands for Relational Database Management System.
RDBMS is a database management system (DBMS) that is based
on the relational model. Data from relational database can be
accessed using Structured Query Language (SQL)
1 CREATE VIEW view_name AS SELECT column_name1, column_name2 FROM table_name WHERE CONDITION;
INNER JOIN
LEFT JOIN
RIGHT JOIN
OUTER JOIN
E.g. ‘Age’ field should contain only the value greater than 18.
1 CREATE TABLE EMP_DETAILS(EmpID int NOT NULL, NAME VARCHAR (30) NOT NULL, Age INT CHECK
(AGE > 18), PRIMARY KEY (EmpID));
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
Atomicity
Consistency
Isolation
Durability
DELETE TRUNCATE
rows from a table. It can be rolled the rows from the table and free the
rolled back.
We can delete specific rows using We can only delete all the rows at a time
Delete maintains log and performance Truncate maintains minimal log and
Both Union and Union All concatenate the result of two tables but
the way these two queries handle duplicates are different.
Performance wise Union All is faster than Union, Since Union All
doesn’t remove duplicates. Union query checks the duplicate
values which consumes some time to remove the duplicate
records.
Assume: Table1 has 10 records, Table2 has 10 records. Last
record from both the tables are same.
2 UNION
2 UNION ALL
Data type of all the columns in the two tables should be same.
1 INSERT into Employee_Details (Employee_Name, Salary, Age) VALUES (‘John’, 5500 , 29);
59. How to add a column ‘Salary’ to a table
Employee_Details?
1 USE TestDB
2 GO
4 GO
1 --------------
2 | TestTable1 |
3 --------------
4| 11 |
5| 12 |
6| 13 |
7| 14 |
8 --------------
1 --------------
2 | TestTable2 |
3 --------------
4| 11 |
5| 12 |
6 --------------
1 SELECT GetDate();
1 | Employee_Name | Salary|
2 -----------------------------
3 | John | 2500 |
4 | Emma | 3500 |
5 | Mark | 5500 |
6 | Anne | 6500 |
7 -----------------------------
Syntax:
Output:
1 | Employee_Name | Salary|
2 -----------------------------
3 | Mark | 5500 |
4 | Anne | 6500 |
5 -----------------------------
1 | Employee_Name | Salary|
2 -----------------------------
3 | John | 2500 |
4 | Emma | 3500 |
5 | Mark | 5500 |
6 | Anne | 6500 |
7 -----------------------------
Syntax:
Output:
1 | Employee_Name | Salary|
2 -----------------------------
3 | Emma | 3500 |
4 -----------------------------
1 sp_rename OldTableName,NewTableName
1 | Gender |
2 ------------
3 | M |
4 | F |
5 | NULL |
6 | m |
7 | f |
8 | g |
9 | H |
10 | i |
11 ------------
1 SELECT Gender,
2 case
7 else upper(Gender)
1 select case when null = null then 'True' else 'False' end as Result;
1 select case when null is null then 'True' else 'False' end as Result;
1 select case when null is null then 'Queries In SQL Server' else 'Queries In MySQL' end as Result;
2 ------------------------
3 | John | M |
4 | Emma | F |
5 | Mark | M |
6 | Anne | F |
7 ------------------------
1 UPDATE TestTable SET Gender = CASE Gender WHEN 'F' THEN 'M' ELSE 'F' END
ORACLE:
MySQL:
SQL Server:
1 SELECT col1 * (col2 + ISNULL(col3,0)) FROM Table1