[go: up one dir, main page]

0% found this document useful (0 votes)
27 views12 pages

SQL - Interview Questions 1 - Mangesh Sir

The document provides a comprehensive overview of databases, including definitions of key concepts such as databases, data warehouses, tables, fields, records, and SQL commands. It explains various types of SQL commands (DDL, DML, DCL, TCL), joins, constraints, and operators, along with practical SQL examples for data manipulation. Additionally, it covers the differences between commands like DELETE, TRUNCATE, and DROP, as well as the use of aggregate functions and subqueries.

Uploaded by

dheerajk3435
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

SQL - Interview Questions 1 - Mangesh Sir

The document provides a comprehensive overview of databases, including definitions of key concepts such as databases, data warehouses, tables, fields, records, and SQL commands. It explains various types of SQL commands (DDL, DML, DCL, TCL), joins, constraints, and operators, along with practical SQL examples for data manipulation. Additionally, it covers the differences between commands like DELETE, TRUNCATE, and DROP, as well as the use of aggregate functions and subqueries.

Uploaded by

dheerajk3435
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Prepared By- Mangesh Sir

VCTC Katraj, Pune

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.

2. What is Data warehouse?


Data warehouse refers to a central repository of data from multiple sources of information. Those
data are consolidated, transformed and made available for the mining as well as online
processing.

3. What is a Table in a Database?


A table is a database object used to store records in a field in the form of columns and rows that
holds data.

4. What is a Field in a Database?


A field in a Database table is a space allocated to store a particular record within a table.

5. What is a Record in a Database?


A record (also called a row of data) is an ordered set of related data in a table.

6. What is a column in a Table?


A column is a vertical entity in a table that contains all information associated with a specific
field in a table.

7. 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.

8. What are the types of DBMS?


There are two types of DBMS
1. Relational Database Management System (RDBMS)
2. Non-Relational Database Management System

9. What is RDBMS?
Prepared By- Mangesh Sir
VCTC Katraj, Pune
RDBMS stands for Relational Database Management System. RDBMS is a database
management system (DBMS) that is based on the relational model. Data from a relational
database can be accessed using Structured Query Language (SQL)

10. What are the popular Database Management Systems in the IT Industry?
Oracle, MySQL, Microsoft SQL Server, PostgreSQL, Sybase, MongoDB, DB2, and Microsoft
Access etc.

11. What is SQL?


SQL Overview: SQL stands for Structured Query Language. It is an American National Standard
Institute (ANSI) standard. It is a standard language for accessing and manipulating databases.
Using SQL, some of the action we could do are to create databases, tables, stored procedures
(SP’s), execute queries, retrieve, insert, update, delete data against a database.

12. What are the different types of SQL commands?


SQL commands are segregated into the following types:

 DDL – Data Definition Language


 DML – Data Manipulation Language
 DQL – Data Query Language
 DCL – Data Control Language
 TCL – Transaction Control Language

13. What are the different DDL commands in SQL?


DDL commands are used to define or alter the structure of the database.

 CREATE: To create databases and database objects


 ALTER: To alter existing database objects
 DROP: To drop databases and databases objects
 TRUNCATE: To remove all records from a table but not its database structure
 RENAME: To rename database objects

14. What are the different DML commands in SQL?


DML commands are used for managing data present in the database.

 SELECT: To select specific data from a database


 INSERT: To insert new records into a table
 UPDATE: To update existing records
Prepared By- Mangesh Sir
VCTC Katraj, Pune
 DELETE: To delete existing records from a table

15. What are the different DCL commands in SQL?


DCL commands are used to create roles, grant permission, and control access to the database
objects.

 GRANT: To provide user access


 DENY: To deny permissions to users
 REVOKE: To remove user access

16. What are the different TCL commands in SQL?


TCL commands are used to manage the changes made by DML statements.

 COMMIT: To write and store the changes to the database


 ROLLBACK: To restore the database since the last commit

17. What is a View in SQL?


A view is like a subset of a table which is stored logically in a database. A view is a virtual table.
It contains rows and columns similar to a real table. The fields in the view are fields from one or
more real tables. Views do not contain data of their own. They are used to restrict access to the
database or to hide data complexity.

18. What are the advantages of Views?


Some of the advantages of Views are

1. Views occupy no space


2. Views are used to simply retrieve the results of complicated queries that need to be
executed often.
3. Views are used to restrict access to the database or to hide data complexity.

19. What is a query?


A database query is a request for data or information from a database table or combination of
tables. A database query can be either a select query or an action query.

20. What is a Subquery?


A Subquery is a SQL query within another query. It is a subset of a Select statement whose
return values are used in filtering the conditions of the main query.
Prepared By- Mangesh Sir
VCTC Katraj, Pune
21. What are the types of subquery?
There are two types of subquery:

1. Correlated: In a SQL database query, a correlated subquery is a subquery that uses values
from the outer query in order to complete. Because a correlated subquery requires the outer
query to be executed first, the correlated subquery must run once for every row in the outer
query. It is also known as a synchronized subquery.

2. Non-Correlated: A Non-correlated subquery is a subquery in which both outer query and


inner query are independent to each other.

22. How to avoid duplicate records in a query?


The SQL SELECT DISTINCT query is used to return only unique values. It eliminates all the
duplicated values.

23. What is the difference between Rename and Alias?


‘Rename’ is a permanent name given to a table or column
‘Alias’ is a temporary name given to a table or column.

24. What is a Join?


Join is a query, which retrieves related columns or rows from multiple tables.

25. What are the different types of joins?


Types of Joins are as follows:

 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 OUTER JOIN

26. What is the difference between an inner and outer join?


Inner Join: An inner join returns rows when there is at least some matching data between two
(or more) tables that are being compared.

Outer Join: An outer join returns rows from both tables that include the records that are
unmatched from one or both the tables.

27. What are SQL constraints?


Prepared By- Mangesh Sir
VCTC Katraj, Pune
SQL constraints are the set of rules that enforced some restriction while inserting, deleting or
updating of data in the databases.

28. What are the constraints available in SQL?


Some of the constraints in SQL are – Primary Key, Foreign Key, Unique Key, SQL Not Null,
Default, Check and Index constraint.

29. What is a Unique constraint?


A unique constraint is used to ensure that there are no duplication values in the field/column.

30. What is a Primary Key?


A PRIMARY KEY constraint uniquely identifies each record in a database table. All columns
participating in a primary key constraint must not contain NULL values.

31. Can a table contain multiple PRIMARY KEY’s?


The short answer is no, a table is not allowed to contain multiple primary keys but it allows to
have one composite primary key consisting of two or more columns.

32. What is a Composite PRIMARY KEY?


Composite PRIMARY KEY is a primary key created on more than one column (combination of
multiple fields) in a table.

33. What is a FOREIGN KEY?


A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY in a table is
linked with the PRIMARY KEY of another table.

34. Can a table contain multiple FOREIGN KEY’s?


A table can have many FOREIGN KEY’s.

35. What is the difference between UNIQUE and PRIMARY KEY constraints?
There should be only one PRIMARY KEY in a table whereas there can be any number
of UNIQUE Keys.
PRIMARY KEY doesn’t allow NULL values whereas Unique key allows NULL values.

36. What is a NULL value?


Prepared By- Mangesh Sir
VCTC Katraj, Pune
A field with a NULL value is a field with no value. A NULL value is different from a zero value
or a field that contains spaces. A field with a NULL value is one that has been left blank during
record creation. Assume, there is a field in a table is optional and it is possible to insert a record
without adding a value to the optional field then the field will be saved with a NULL value.

37. What is the difference between NULL value, Zero, and Blank space?
As I mentioned earlier, Null value is field with no value which is different from zero value and
blank space.
Null value is a field with no value.
Zero is a number
Blank space is the value we provide. The ASCII value of space is CHAR(32).

38. How to Test for NULL Values?


A field with a NULL value is a field with no value. NULL value cannot be compared with other
NULL values. Hence, It is not possible to test for NULL values with comparison operators, such
as =, <, or <>. For this, we have to use the IS NULL and IS NOT NULL operators.

39. What is SQL NOT NULL constraint?


NOT NULL constraint is used to ensure that the value in the filed cannot be a NULL

40. What is SQL NOT NULL constraint?


NOT NULL constraint is used to ensure that the value in the filed cannot be a NULL

41. What is a CHECK constraint?


A CHECK constraint is used to limit the value that is accepted by one or more columns.

E.g. ‘Age’ field should contain only the value greater than 18.

42. What is a DEFAULT constraint?


DEFAULT constraint is used to include a default value in a column when no value is supplied at
the time of inserting a record.

43. What are Operators available in SQL?


SQL Operator is a reserved word used primarily in an SQL statement’s WHERE clause to
perform operations, such as arithmetic operations and comparisons. These are used to specify
conditions in an SQL statement.
Prepared By- Mangesh Sir
VCTC Katraj, Pune
There are three types of Operators.

1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators

42. Define the SELECT INTO statement.


The SELECT INTO statement copies data from one table into a new table. The new table will be
created with the column-names and types as defined in the old table. You can create new column
names using the AS clause.

45. What is the difference between Delete, Truncate and Drop command?
The difference between the Delete, Truncate and Drop command is

 Delete command is a DML command, it is used to delete rows from a table. It can be
rolled back.
 Truncate is a DDL command, it is used to delete all the rows from the table and free the
space containing the table. It can’t be rolled back.
 Drop is a DDL command, it removes the complete data along with the table structure
(unlike truncate command that removes only the rows). All the tables’ rows, indexes, and
privileges will also be removed.

46. What is the difference between Delete and Truncate?


 The difference between the Delete, and Truncate are

DELETE TRUNCATE

Truncate statement is used to delete all the


Delete statement is used to delete rows from
rows from the table and free the space
a table. It can be rolled back.
containing the table. It can’t be rolled back.

We can’t use WHERE condition in


We can use WHERE condition in DELETE
TRUNCATE statement. So we can’t delete
statement and can delete required rows
required rows alone
Prepared By- Mangesh Sir
VCTC Katraj, Pune

DELETE TRUNCATE

We can only delete all the rows at a time


We can delete specific rows using DELETE
using TRUNCATE

Delete is a DML command Truncate is a DDL command

Delete maintains log and performance is Truncate maintains minimal log and
slower than Truncate performance wise faster

We need DELETE permission on Table to We need at least ALTER permission on the


use DELETE command table to use TRUNCATE command

47. What is the difference between Union and Union All command?
This is one of the tricky SQL Interview Questions. Interviewer may ask you this question in
another way as what are the advantages of Union All over Union.

Both Union and Union All concatenate the result of two tables but the way these two queries
handle duplicates are different.

Union: It omits duplicate records and returns only distinct result set of two or more select
statements.
Union All: It returns all the rows including duplicates in the result set of different select
statements.

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.

48. What is CLAUSE in SQL?


SQL CLAUSE helps to limit the result set by providing a condition to an SQL Query. A
CLAUSE helps to filter the rows from the entire set of records. SQL CLAUSES are WHERE &
HAVING.

49. What is the difference between Having and Where clause?


Prepared By- Mangesh Sir
VCTC Katraj, Pune
Where clause is used to fetch data from a database that specifies particular criteria whereas a
Having clause is used along with ‘GROUP BY’ to fetch data that meets particular criteria
specified by the Aggregate functions. Where clause cannot be used with Aggregate functions,
but the Having clause can.

50. What are aggregate functions in SQL?


SQL aggregate functions return a single value, calculated from values in a column. Some of the
aggregate functions in SQL are as follows

 AVG() – This function returns the average value


 COUNT() – This function returns the number of rows
 MAX() – This function returns the largest value
 MIN() – This function returns the smallest value
 ROUND() – This function rounds a numeric field to the number of decimals specified
 SUM() – This function returns the sum

51. What is Self-Join?


A self-join is a join in which a table is joined with itself, especially when the table has a Foreign
Key which references its own Primary Key.

52. What is Cross-Join?


Cross join produces a result set which is the number of rows in the first table multiplied by a
number of rows in the second table if no WHERE clause is used along with Cross join. This kind
of result is known as Cartesian Product. If suppose, Where clause is used in cross join then the
query will work like an Inner join.

53. How to get unique records from a table?


By using DISTINCT keyword, we can get unique records from a table

SELECT DISTINCT column name FROM table name;

54. How to add new Employee details in an Employee_Details table with the following
details
Employee_Name: John, Salary: 5500, Age: 29?
Prepared By- Mangesh Sir
VCTC Katraj, Pune
INSERT INTO Employee_Details (Employee_Name, Salary, Age)
VALUES(‘John’,5500,29);

55. How to add a column ‘Salary’ to a table Employee_Details?


ALTER Empolyee_Details
ADD Salary int;

56. How to change a value of the field ‘Salary’ as 7500 for an Employee_Name ‘John’ in a
table Employee_Details?
UPDATE Employee Details
SET Salary=7500
WHERE Empolyee_Name =’John’;

57. Write an SQL Query to select all records from the table?
SELECT * FROM tablename;

58. Define SQL Delete statement.


The SQL Delete statement is used to delete records from a table.

59. Write the command to remove all Players named Sachin from the Players table.
DELETE FROM Players WHERE Players_Name = ‘Sachine’;

60. How to get each name only once from an employee table?
By using the DISTINCT keyword, we could get each name only once.
Syntax
SELECT DISTINCT column name FROM table name;

61. How to rename a column in the output of SQL query?


By using SQL AS keyword
SELECT old column name AS new column name
FROM table name;

62. What is the order of SQL SELECT?


Order of SQL SELECT statement is as follows
SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.

63. How to display the current date in SQL?


In SQL, there is a built-in function called GetDate() which helps to return the current date.
SELECT GetDate();
Prepared By- Mangesh Sir
VCTC Katraj, Pune
64. Write an SQL Query to find an Employee_Name whose Salary is equal or greater than
5000 from the below table Employee_Details.

Syntax

SELECT Empolyee_Name, Salary FROM Empolyee_Details

WHERE Salary>=5000;

65. Write an SQL Query to find list of Employee_Name start with ‘E’ from the below table
SELECT * FROM Empolyee_Details
WHERE Empolyee_Name LIKE ‘E%’;

66. Write SQL SELECT query that returns the FirstName and LastName from
Employee_Details table.
SELECT FirstName, LastName FROM Empolyee_Details;

67. How to rename a Table?


Sp_rename Old table name, New table name; (Table rename)

Sp_rename ‘Tablename.columname’, ’Newcolumnname’; (Column rename)

68. How to select all the even number records from a table?
To select all the even number records from a table:

SELECT * FROM table name


WHERE id % 2 = 0;
Prepared By- Mangesh Sir
VCTC Katraj, Pune
69. How to select all the odd number records from a table?
To select all the odd number records from a table:

SELECT * FROM table name


WHERE id % 2 !=0;

70. What is Database Testing?


It is also called as back-end testing or data testing.
Database testing involves in verifying the integrity of data in the front end with the data present
in the back end. It validates the schema, database tables, columns, indexes, stored
procedures, triggers, data duplication, orphan records, junk records. It involves in updating
records in a database and verifying the same on the front end.

71. What is the difference between GUI Testing and Database Testing?

 GUI Testing is AKA User Interface Testing or Front-end testing


Database Testing is AKA back-end testing or data testing.
 GUI Testing deals with all the testable items that are open to the user to interaction such
as Menus, Forms etc.
Database Testing deals with all the testable items that are generally hidden from the user.
 The tester who is performing GUI Testing doesn’t need to know Structured Query
Language
The tester who is performing Database Testing needs to know Structured Query
Language
 GUI Testing includes invalidating the text boxes, check boxes, buttons, drop-downs,
forms etc., majorly the look and feel of the overall application
Database Testing involves in verifying the integrity of data in the front end with the data
present in the back end. It validates the schema, database tables, columns, indexes, stored
procedures, triggers, data duplication, orphan records, junk records. It involves in
updating records in a database and verifying the same on the front end.

You might also like