0% found this document useful (0 votes)
3 views

SQL_Questions_Answers

Uploaded by

bhoomi9122singh
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)
3 views

SQL_Questions_Answers

Uploaded by

bhoomi9122singh
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/ 6

1. What is the difference between BETWEEN and IN operators in SQL?

BETWEEN: Used to filter records within a specified range.


Example:

SELECT * FROM Students WHERE marks BETWEEN 50 AND 100;

● Retrieves students with marks between 50 and 100 (inclusive).

IN: Used to filter records matching a specific list of values.


Example:

SELECT * FROM Students WHERE grade IN ('A', 'B', 'C');

● Retrieves students with grades 'A', 'B', or 'C'.

2. What is a join in SQL? What are the types of joins?

A join combines rows from two or more tables based on a related


column.

● Types of Joins:

Inner Join: Returns matching records from both tables.


SELECT * FROM Orders INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;

1.

Left Join (or Left Outer Join): Returns all records from the left
table and matching records from the right.
SELECT * FROM Orders LEFT JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;

2.

Right Join (or Right Outer Join): Returns all records from the right
table and matching records from the left.
SELECT * FROM Orders RIGHT JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;
3.

Full Join (or Full Outer Join): Returns all records when there is a
match in either table.
SELECT * FROM Orders FULL JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;

4.

3. What is the ON DELETE CASCADE constraint?

This constraint ensures that when a record in the parent table is


deleted, the corresponding records in the child table are also
deleted automatically.

Example:

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON
DELETE CASCADE
);

If a customer is deleted from the Customers table, their orders are


also removed from the Orders table.

4. What is the difference between SQL DELETE and SQL TRUNCATE


commands?
DELETE: Removes specific rows based on a condition. Can be rolled
back. DML
Example:

DELETE FROM Students WHERE grade = 'F';

TRUNCATE: Removes all rows from a table. Cannot be rolled back. DDL
Example:
TRUNCATE TABLE Students;

5. Name the function used to remove spaces at the end of a string.


RTRIM(): Removes trailing spaces from a string.
Example:
SELECT RTRIM('Hello ') AS TrimmedString;

6. Explain the SQL HAVING statement.

The HAVING clause filters groups created by the GROUP BY clause


based on a condition.

Example:

SELECT department, AVG(salary)


FROM Employees
GROUP BY department
HAVING AVG(salary) > 50000;

Retrieves departments where the average salary exceeds 50,000.

7. Are NULL values the same as zero or a blank space?

No, NULL represents the absence of a value, while zero (0) and blank
space (' ') are actual values.

Example:
SELECT * FROM Students WHERE age IS NULL;

8. Difference between NVL and NVL2 functions?


NVL(expr1, expr2): Replaces NULL with a default value.
Example:

SELECT NVL(salary, 0) FROM Employees;

NVL2(expr1, expr2, expr3): Returns expr2 if expr1 is not NULL, else


returns expr3.
Example:

SELECT NVL2(bonus, 'Eligible', 'Not Eligible') FROM Employees;

9. What is a foreign key?

A foreign key links one table to another by referencing a primary


key in the second table.

Example:

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

10. What is self-join and what is its requirement?

A self-join joins a table with itself, often using aliases.

Example:

SELECT A.EmployeeID, A.Name, B.Name AS ManagerName


FROM Employees A
JOIN Employees B ON A.ManagerID = B.EmployeeID;

Used to find hierarchical relationships, e.g., employees and their


managers.
11. How to write an SQL query to find students' names starting with
'A'?
SELECT * FROM Students WHERE Name LIKE 'A%';

12. Does the following query return output?


WHERE AVG(marks) > 70
SELECT subject_code, AVG(marks)
FROM Students
GROUP BY subject_code;

No, this query is invalid because the WHERE clause cannot use
aggregate functions. Use HAVING instead:

SELECT subject_code, AVG(marks)


FROM Students
GROUP BY subject_code
HAVING AVG(marks) > 70;

13. Differences between SQL and NoSQL databases?

Feature SQL NoSQL

Data Relational Non-relational (JSON,


Model (tables) key-value, graph)

Schema Fixed Flexible

Examples MySQL, MongoDB, Cassandra


PostgreSQL

14. What are some SQL constraints you know?

1. PRIMARY KEY: Ensures unique identification of each record.


2. FOREIGN KEY: Maintains referential integrity.
3. NOT NULL: Ensures no NULL values.
4. UNIQUE: Ensures all values in a column are unique.
5. CHECK: Validates a condition for each record.
15. Write an SQL query to print details of students whose FIRST_NAME
ends with 'a' and contains six alphabets.
SELECT * FROM Students
WHERE FIRST_NAME LIKE '_____a';

The underscore (_) represents one character.

You might also like