996 SQL Verses Comparisons

IDEABOX:
FOUND ON INTERNET: (Existing Interview Questions!)


Table of Contents

Full Join VS Inner Join

A FULL JOIN in SQL returns all rows from both tables, including rows that do not match in either table, filling non-matching columns with NULLs, while an INNER JOIN only returns the rows where there is a match in both tables based on the join condition.[1][3][5]

FULL JOIN Explained

INNER JOIN Explained

Comparison Table

Feature INNER JOIN FULL JOIN
Matched Rows Shown Shown
Unmatched Rows Not shown Shown (with NULLs for missing)
Use Cases Strict intersection of tables Complete union of tables
Output Only related data All data, matched and unmatched

Each join serves a different purpose, so the choice depends on whether only matched records or every record from both tables (matched or not) is needed.[4][5][1]

1
2
3
4
5
6
7
8


Self Join VS Inner Join

A SELF JOIN in SQL refers to joining a table to itself to compare or relate rows within the same table, whereas an INNER JOIN typically refers to joining two different tables to find matching records based on a specified condition.[1][2][5]

SELF JOIN Explained

INNER JOIN Explained

Key Differences Table

Feature INNER JOIN SELF JOIN
Tables Involved Usually two or more distinct tables [2] Same table joined to itself [1][5]
Main Purpose Match related rows between tables [8] Compare or relate rows within a table [1][5]
Common Use Case Employee to department linking [2] Employee-manager hierarchy [1][5]
Aliases Used Optional when joining different tables [4] Required for differentiating same table instances [1][6]
Join Operators INNER, LEFT, etc. [2][8] Any join operator, typically INNER [1][2]

In summary, a SELF JOIN targets relationships within a table, while INNER JOIN usually merges related records across tables.A SELF JOIN in SQL is when a table is joined to itself to find relationships within its own data, while an INNER JOIN combines rows from two tables where there is a match based on specified conditions.[1][5][2]

SELF JOIN

INNER JOIN

Key Differences

Feature INNER JOIN SELF JOIN
Tables Two or more distinct tables [2] Same table (with aliases) [1]
Application Relate different datasets [2] Compare rows within table [1]
Use case Employee-Department linking [2] Employee-Manager hierarchy [1]
Aliases Optional [2] Required [1][6]

A SELF JOIN is literally an INNER JOIN (or LEFT JOIN, etc.) carried out on a single table. The real distinction is that INNER JOIN usually means "join across tables," while SELF JOIN means "join within a table" using aliases.[2][5][1]

1
2
3
4
5
6
7
8
9


Complex GROUP BY + WHERE OR HAVING SYNTAXES

Complex SQL queries often combine GROUP BY with WHERE and HAVING clauses to filter data before and after aggregation. The WHERE clause filters rows before grouping, while HAVING filters groups after aggregation—commonly using aggregate functions like COUNT, SUM, or AVG.[1][3][6]

GROUP BY with WHERE and HAVING

Syntax Pattern

SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1
HAVING aggregate_condition;

Multiple Conditions (Complex HAVING/WHERE)

Notes on Usage

Complex GROUP BY queries build nuanced reports or dashboards by cleverly combining WHERE and HAVING filters using AND/OR logic—not just restricting, but shaping how summary insights emerge from raw data.[5][7]

1
2
3
4
5
6
7
8
9


Here is a sample SQL query that combines WHERE, GROUP BY, and HAVING clauses to produce a filtered summary report:[1][2][3]

SELECT department, COUNT(employee_id) AS num_employees, AVG(salary) AS avg_salary
FROM employees
WHERE hire_date >= '2024-01-01'
GROUP BY department
HAVING COUNT(employee_id) > 5 AND AVG(salary) > 40000;

This type of query is often used for analytical summaries where both pre-aggregation and post-aggregation filters are required.

1
2
3


ANY VS ALL VS IN

The SQL operators ANY, ALL, and IN are used to compare a value with a set or subquery results, but each works differently in evaluating conditions.[1][2][4]

ANY Operator

ALL Operator

IN Operator

Comparison Table

Operator Use Case Result if condition is true for
 Example Syntax
ANY Compare against at least one value At least one value in subquery WHERE age > ANY (SELECT age ...)
ALL Compare against all values Every value in subquery WHERE age > ALL (SELECT age ...)
IN List or subquery equality check Any value in the list or subquery WHERE status IN ('open', 'closed')

ANY and ALL are flexible for multiple comparison operators, while IN is equivalent to “= ANY” and is limited to equality checks.[4][1][7]

1
2
3
4
5
6
7
8
9
10


SELECT * INTO VS INSERT INTO SELECT

SELECT * INTO creates a new table and copies data from an existing table, while INSERT INTO SELECT inserts data into an already existing table from the results of a SELECT query[1][3][5].

SELECT INTO

INSERT INTO SELECT

Differences Table

Feature SELECT INTO INSERT INTO SELECT
Table Creation Creates new table automatically [3] Requires target table to exist [1]
When to Use For quick backups, temporary and ad-hoc tables Copying data to existing schemas
Syntax Simplicity Simple for new tables Simple for existing tables
Constraints/Catalog No keys, indexes, constraints by default [1] Keeps target table structure as defined
Performance Often faster for large data transfers [4] Can be slower if indexes/triggers exist

In summary, SELECT INTO is ideal for creating new tables from queries or subsets, while INSERT INTO SELECT is best for moving or copying data when the structure is pre-defined.[1][3][5]

1
2
3
4
5
6
7
8
9


IS NULL VS NULL VS NOT NULL

In SQL, IS NULL, NULL, and IS NOT NULL have specific meanings and uses. NULL is a marker indicating a missing value, IS NULL is used to check for such missing values, and IS NOT NULL checks for the presence of a value.[2][7][8]

NULL

IS NULL and IS NOT NULL

NULL in Comparisons

Summary Table

SQL keyword Meaning Typical Usage & Meaning
NULL Absence of any value Used in INSERT/UPDATE to set missing data [7]
IS NULL Test for missing value WHERE column IS NULL (column contains NULL marker) [2]
IS NOT NULL Test for existing value WHERE column IS NOT NULL (column contains some value) [8]
= NULL / <>NULL Invalid comparison NULL does not equal NULL; these comparisons always fail [9][3]

In summary: use IS NULL/IS NOT NULL to filter records with or without values, and remember that NULL means "missing" or "unknown," not empty or zero.[7][8][2]

1
2
3
4
5
6
7
8
9