SQL Interview Questions with Answers: SQL stands for Structured Query Language, and it is a standard programming language used for managing and manipulating relational databases. SQL provides a way to create, update, and delete data in a database, as well as retrieve and manipulate data.
SQL uses a set of commands to interact with a database, including SELECT, INSERT, UPDATE, and DELETE. These commands are used to retrieve, insert, modify, and delete data from a database.
In addition to these basic commands, SQL also includes advanced features like JOINs, subqueries, stored procedures, and triggers. These features help to make SQL a powerful tool for working with databases.
SQL is used by a wide range of applications and services, including web applications, mobile apps, and enterprise software. It is a core technology for managing data in the digital age and is essential for working with large amounts of data in a structured and organized way.
SQL Interview Questions with Answers:
Q.1 What is SQL?
Answer: SQL stands for Structured Query Language, and it is a standard programming language used for managing and manipulating relational databases.
Q.2 What is a Primary Key?
Answer: A primary key is a field in a database table that uniquely identifies each record in that table. It must be unique and cannot contain null values.
Q.3 What is a Foreign Key?
Answer: A foreign key is a field in a database table that refers to the primary key of another table. It is used to establish a relationship between two tables.
Q.4 What is a JOIN?
Answer: JOIN is used to combine rows from two or more tables based on a related column between them.
Q.5 What is normalization in SQL?
Answer: Normalization is the process of organizing data in a database so that it is consistent and reduces redundancy. It helps to avoid data inconsistencies and makes the database more efficient.
Q.6 What is a stored procedure?
Answer: A stored procedure is a set of SQL statements that are stored in the database and can be executed repeatedly. It is used to encapsulate complex logic that can be reused multiple times.
Q.7 What is a trigger?
Answer: A trigger is a set of SQL statements that are automatically executed when a specific event occurs in the database. It is used to enforce business rules and data integrity.
Q.8 What is a subquery?
Answer: A subquery is a query nested inside another query. It is used to retrieve data that will be used in the main query.
Q.9 What is the difference between UNION and UNION ALL?
Answer: UNION removes duplicate rows while UNION ALL includes duplicate rows.
Q.10 What is an Index in SQL?
Answer: An index is a database object used to improve the performance of SQL queries. It allows data to be accessed more quickly by creating a copy of the table with a specific order to optimize search speed.
Java Interview Questions Answers –
- Top 10 Core Java Interview Question Answer
- 25 Java Interview Questions and Answer
- Spring Boot interview Questions with Answers [Top10 ]
- JSP interview Questions and Answers for Freshers[10]
- How to Connect Mysql Database in Java using Spring Boot
- Spring boot tricky interview questions [10]
- Difference Between JSP and Servlets
- Microservices interview questions with Answers
SQL Query for Second highest salary
To retrieve the second highest salary from a table, you can use the following SQL query:
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
This query uses a subquery to find the maximum salary in the table, and then selects the maximum salary that is less than the maximum salary found in the subquery. This will give you the second highest salary in the table.
Assuming that the table is named “employees” and has a column named “salary” that contains the salaries of employees. You can replace “employees” with the actual name of your table, and “salary” with the actual name of your column.
SQL Query for find duplicate records
To find duplicate records in a table, you can use the following SQL query:
SELECT column1, column2, column3, COUNT(*) as count
FROM table_name
GROUP BY column1, column2, column3
HAVING COUNT(*) > 1;
Replace table_name
with the name of the table you want to search for duplicate records in, and column1
, column2
, and column3
with the names of the columns you want to check for duplicates.
The GROUP BY
clause groups the records by the specified columns, and the HAVING
clause filters the groups to only show those with a count greater than 1.
This query will return all the duplicate records in the table, along with a count of how many times each set of duplicated values appears in the table. You can remove the COUNT(*) as count
line if you only want to see the duplicate records themselves.
How to find Duplicate Records in SQL with Condition
To find duplicate records in SQL with a condition, you can modify the previous query with a WHERE
clause that specifies the condition you want to filter by. Here’s an example:
SELECT column1, column2, COUNT(*) as count
FROM table_name
WHERE condition
GROUP BY column1, column2
HAVING COUNT(*) > 1;
Replace table_name
with the name of the table you want to search for duplicate records in, and column1
and column2
with the names of the columns you want to check for duplicates. Replace condition
with the condition you want to filter by.
The WHERE
clause filters the records based on the specified condition, and the GROUP BY
clause groups the remaining records by the specified columns. The HAVING
clause filters the groups to only show those with a count greater than 1.
This query will return all the duplicate records in the table that meet the specified condition, along with a count of how many times each set of duplicated values appears in the table. You can remove the COUNT(*) as count
line if you only want to see the duplicate records themselves.
SQL Query to insert records from another table
To insert records from one table into another table, you can use the following SQL query:
INSERT INTO table1 (column1, column2, column3)
SELECT column1, column2, column3
FROM table2
WHERE condition;
Replace table1
with the name of the table you want to insert the records into, and column1
, column2
, and column3
with the names of the columns you want to insert data into. Replace table2
with the name of the table you want to select data from. Replace condition
with the condition you want to filter by.
This query selects the data from table2
based on the specified condition, and inserts it into table1
. The columns specified in the INSERT INTO
clause must match the columns selected in the SELECT
clause.
This query can be useful for copying data from one table to another, or for consolidating data from multiple tables into a single table. Note that the data types of the columns in both tables should be compatible for this query to work properly.
Join Telegram | Join Now |
Home Page | Full Stack With Java |
PSA Student Join | click here |