100 SQL Questions With Real Examples-2
100 SQL Questions With Real Examples-2
Here are Top SQL Interview Questions for Beginners with Clear Example
4) Difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN?
Example:
SELECT *
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.id;
• IN – Specific values
51) What is the difference between TEMP and GLOBAL TEMP tables?
• TEMP: Session-specific
• GLOBAL TEMP: Visible to all sessions (until last session disconnects)
CREATE TEMP TABLE temp_sales (...);
52) What is the difference between IN, ANY, ALL, and EXISTS?
-- IN: list match
-- ANY: at least one match
-- ALL: all must match
-- EXISTS: subquery returns at least one row
53) How to find employees with no department?
SELECT *
FROM employees
WHERE dept_id IS NULL;
-- MySQL
SELECT name, TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age FROM employees;
-- MySQL
SELECT REVERSE('LinkedIn');
-- SQL Server
EXEC sp_help 'employees';
82) What is the difference between HAVING and WHERE with example?
• WHERE filters rows
85) Difference between LEFT OUTER JOIN and RIGHT OUTER JOIN?
• LEFT returns all from left + matches from right
• RIGHT does the opposite
86) How to join 3 or more tables?
SELECT *
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN products p ON o.product_id = p.id;
-- SQL Server
CAST('2025-07-08' AS DATE);
93) How to create a running total using CTE?
WITH salary_cte AS (
SELECT id, salary,
SUM(salary) OVER (ORDER BY id) AS running_total
FROM employees
)
SELECT * FROM salary_cte;
-- PostgreSQL
SELECT pg_size_pretty(pg_total_relation_size('employees'));
100) What is the difference between delete and truncate with foreign keys?
• DELETE: Allowed with FK (if CASCADE or FK exists)