Most Asked SQL Queries in Interview (1)
Most Asked SQL Queries in Interview (1)
Solution:
FROM employees
LIMIT 1 OFFSET 2;
OR
FROM employees
SELECT MAX(salary)
FROM employees
SELECT MAX(salary)
FROM employees
);
2. Find out employees name who are getting 2nd highest salary.(More than 1 employee is getting
second highest salary)
Solution:
FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
FROM employees
);
Solution:
SELECT
full_name,
FROM employees;
Solution:
FROM table_name
Solution:
WHERE id NOT IN (
SELECT MIN(id)
FROM table_name
GROUP BY columnname
);
6.Get the list of the employees who joined in the last 6 months.
Solution:
SELECT *
FROM employees
Solutions:
FROM employees e1
JOIN employees e2
ON e1.id = e2.manager_id;
Solution:
FROM employees
GROUP BY department_id;
Solution:
FROM employees
GROUP BY department_id;
Solution:
SELECT *
FROM employees
Solution:
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
Solution:
SELECT *
FROM employees
Solution:
SELECT *
FROM employees
ORDER BY id DESC
LIMIT 5;
14.Find employees whose names contain exactly two occurrences of the letter 'a'
Solution:
SELECT *
FROM employees
Solution:
FROM employees;
DELETE removes specific rows based on a condition and can be rolled back.
TRUNCATE removes all rows from a table but cannot be rolled back (in most systems)
UNION removes duplicate records, whereas UNION ALL includes all records.
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 Outer Join: Returns all rows when there is a match in either table.
A candidate key is a column, or a combination of columns, in a database table that can uniquely
identify each row in that table. It is a candidate to become the primary key of the table, as it satisfies
the following properties.
Window functions are SQL functions that perform calculations across a set of rows related to the
current row, but unlike aggregate functions, they do not collapse rows into a single output. Instead,
they return a value for each row in the dataset.
Solution:
VALUES
23.How do you delete all records from a table but keep the structure?
Solution:
24.Copy employees with a salary greater than 60,000 into the new_employees table.
Solution:
FROM employees
25.copy only the structure of a table (without copying the data) into a new table
Solution:
AS
SELECT *
FROM existing_table
WHERE 1 <> 1;