SQL_Viva_Questions_and_Answers
SQL_Viva_Questions_and_Answers
1. What is SQL?
Answer: SQL (Structured Query Language) is a standard
programming language used to manage and manipulate relational
databases. It allows users to perform operations like querying,
updating, inserting, and deleting data from a database.
2. What is a primary key?
Answer: A primary key is a column (or combination of columns) in a
table that uniquely identifies each row in that table. It ensures that no
two rows have the same value and does not allow NULL values.
3. What is a foreign key?
Answer: A foreign key is a column in one table that references the
primary key in another table. It is used to establish and enforce a link
between the data in the two tables.
4. What is the difference between WHERE and HAVING?
Answer: WHERE is used to filter rows before grouping data in SQL. It
works on individual rows. HAVING is used to filter groups of data
after the GROUP BY clause has been applied.
5. What are joins in SQL? Explain types of joins.
Answer: Joins are used to combine rows from two or more tables
based on a related column. Types of joins:
- Inner Join: Returns rows that have matching values in both tables.
- Left Join: Returns all rows from the left table and matching rows
from the right table.
- Right Join: Returns all rows from the right table and matching
rows from the left table.
- Full Join: Returns all rows when there is a match in either table.
6. What is the difference between DELETE and TRUNCATE?
Answer: DELETE removes specific rows from a table based on a
condition and can be rolled back. TRUNCATE removes all rows from
a table and cannot be rolled back. It is faster but resets the table
structure.
7. What is the GROUP BY clause used for?
Answer: The GROUP BY clause is used to arrange identical data into
groups. It is often used with aggregate functions like SUM(), AVG(),
COUNT(), etc.
8. How do you create a table in SQL?
Answer: CREATE TABLE Students (ID INT PRIMARY KEY, Name
VARCHAR(50), Age INT, Class VARCHAR(20));
9. What is a query? Give an example.
Answer: A query is a request to retrieve or manipulate data in a
database. Example: SELECT * FROM Students WHERE Age > 18;
10. What is the difference between CHAR and VARCHAR?
Answer: CHAR is a fixed-length string. If the data is shorter than the
defined length, it is padded with spaces. VARCHAR is a
variable-length string. It uses only the space required to store the
data.