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

SQL Real time interview questions and answers

The document outlines the differences between SQL commands and concepts, including the use of DELETE vs TRUNCATE, and the definitions of primary and foreign keys. It provides SQL queries for specific use cases, such as fetching records from one table that are not in another and identifying customers based on order amounts. Additionally, it explains the distinction between RANK and DENSE_RANK functions in handling ties.

Uploaded by

er.evil323476
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

SQL Real time interview questions and answers

The document outlines the differences between SQL commands and concepts, including the use of DELETE vs TRUNCATE, and the definitions of primary and foreign keys. It provides SQL queries for specific use cases, such as fetching records from one table that are not in another and identifying customers based on order amounts. Additionally, it explains the distinction between RANK and DENSE_RANK functions in handling ties.

Uploaded by

er.evil323476
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. difference truncate vs delete.

Use Cases:

 Use DELETE when you want to remove specific rows based on a


condition, and when you need to retain the ability to rollback changes.
 Use TRUNCATE when you want to remove all rows from a table quickly
and efficiently, and when you don't need to retain individual row
deletion information.
2. I want to fetch only the records from the "emp1" table (excluding those that
are also present in "emp2") Write sql query will retrieve only the records from
"emp1" that do not have a matching "employee_id" in "emp2." Here's the
SQL query you can use:

SELECT *
FROM emp1
WHERE employee_id NOT IN (SELECT employee_id FROM emp2);

3. sql differenece between primary key and foregin key

Primary Key:

 A primary key is a column or a set of columns in a table that uniquely


identifies each row in that table.
 It ensures data integrity by enforcing the uniqueness and non-
nullability of the identified values.
 A table can have only one primary key.
 Primary keys are typically used as the basis for creating relationships
with other tables through foreign keys.
 Primary keys are used to maintain data integrity and enforce entity
integrity in a table.

Foreign Key:

 A foreign key is a column or a set of columns in a table that refers to


the primary key of another table.
 It establishes a relationship between two tables, known as the parent
table (referenced table) and the child table (referencing table).
 The foreign key in the child table references the primary key in the
parent table, creating a link between the two tables.
 Foreign keys ensure referential integrity by enforcing the integrity and
consistency of data between related tables.
 Multiple foreign keys can exist in a table, representing different
relationships with different parent tables.
 Foreign keys can be used to define different types of relationships such
as one-to-one, one-to-many, or many-to-many between tables.
4.

5. To find the customer who placed a specific order and the other customers
who have spent the most money

you would need two tables: customers and orders. Assuming the
customers table has columns customer_id and customer_name , and the
orders table has columns order_id, customer_id, and order_amount, you
can use the following SQL query:

This query first retrieves the customer name and order amount for the
specified order using the INNER JOIN between the customers and orders
tables. Then, it combines this result with the customer name and the
maximum order amount for the other orders. The result is sorted in
descending order based on the maximum order amount and limited to the
top 10 customers using the LIMIT keyword.
4. table find duplictes values sql query
5. rank vs denserank
Ans: The main difference is how they handle ties. RANK() can skip ranks in case of
ties, leaving gaps between rank values, while DENSE_RANK() does not skip ranks
for tied values.

You might also like