SQL vs NoSQL
Answer
Imagine you are choosing how to store user profiles for an application. You can either layout the data in fixed, strict spreadsheets or throw them into loose, adaptive text folders.
SQL databases use structured tables with fixed rows and columns, enforcing predefined schemas and strict transactional safety. They shine when you need absolute data consistency and complex query joins.
NoSQL databases skip the tables for flexible formats like documents or key-value stores. They are intentionally built for massive scaling, rapid changes, and handling messy, unstructured data.
Examples:
- SQL systems include MySQL, PostgreSQL, Oracle, and SQL Server.
- NoSQL systems include MongoDB, Cassandra, and Redis.
Interview Tip: Pick SQL for traditional, transactional platforms where structure matters most, and turn to NoSQL when scaling out vast amounts of rapidly changing data.
SQL vs PL/SQL
Answer
Example: Think about the difference between sending a single command to a database versus building an automated script that checks conditions and loops through records.
SQL is a declarative query language designed to request or update data one single statement at a time. It cannot handle variables, loops, or complex programming logic on its own.
PL/SQL is an extension built by Oracle that adds procedural programming capabilities directly on top of SQL. It lets you use variables, loops, conditional IF statements, and handle execution errors.
Example:
A regular SQL query looks like this:
SELECT * FROM employees;
A procedural PL/SQL block looks like this:
BEGIN
UPDATE employees SET salary = salary * 1.1;
END;
Interview Tip: SQL is used to talk to the data directly, while PL/SQL lets you build smart, automated business applications inside the database engine.
DELETE vs TRUNCATE vs DROP
Answer
Imagine cleaning out an office building. You can either throw away specific files from a cabinet, dump out the contents of every cabinet at once, or completely bulldoze the entire building down.
DELETE acts like removing specific files. It uses a WHERE clause to target rows, deletes them one by one, fires database triggers, and can be safely rolled back if you make a mistake.
TRUNCATE acts like dumping the cabinets. It instantly wipes out every single row in the table at lightning speed by dropping the storage pages, bypasses row triggers, and cannot easily be undone once committed.
DROP acts like the bulldozer. It completely removes the entire table structure, its columns, its indexes, and all of its data from the database system permanently.
Interview Tip: Use DELETE to target specific records, TRUNCATE to quickly wipe a table clean for reuse, and DROP to eliminate a table forever.
WHERE vs HAVING
Answer
Example: Imagine managing a warehouse where you first throw out all broken boxes, group the remaining boxes by department, and then filter out any department that has less than one hundred total items.
The WHERE clause filters individual raw rows of data before any grouping calculations ever take place. Because it operates on raw rows, you cannot use aggregate math like SUM or AVG inside it.
The HAVING clause filters the final grouped results after a GROUP BY statement has already run. It is designed specifically to check aggregate calculations on those groups.
Example:
SELECT department_id, AVG(salary)
FROM employees
WHERE salary > 3000
GROUP BY department_id
HAVING AVG(salary) > 5000;
Interview Tip: Remember the sequence: WHERE filters the incoming rows first, then rows are grouped, and finally HAVING filters those calculated groups.
UNION vs UNION ALL
Answer
Example: Imagine stacking two separate lists of customer names together. One list has duplicate names that match the other list, and you have to decide whether to scrub them out.
UNION merges the two result sets together and carefully sifts through the final pile to remove every single duplicate row. This extra sorting work makes it slightly slower.
UNION ALL blindly stacks the two result sets directly on top of each other, keeping every single duplicate record intact. Because it skips the sorting pass, it runs noticeably faster.
Interview Tip: Default to using UNION ALL for maximum query speed unless your application explicitly requires duplicate records to be scrubbed.
CHAR vs VARCHAR/VARCHAR2
Answer
Imagine booking spaces in a parking lot. You can either reserve a giant fixed slot that stays empty if a small car parks there, or use a flexible zone that shrinks and grows to fit the exact size of the vehicle.
CHAR represents a fixed-length text string. If you set it to ten characters but only type three, it pads the remaining space with empty blanks, using up the full storage layout anyway. It is fast for predictable, uniform text.
VARCHAR or VARCHAR2 represents a variable-length text string. If you allocate space for one hundred characters but only type three, it shrinks to store exactly those three characters without wasting memory.
Examples:
- CHAR(10) always consumes ten bytes of disk space.
- VARCHAR2(10) adapts down to match the actual text entered.
Interview Tip: Use CHAR for highly consistent codes like country abbreviations, and stick to VARCHAR2 for standard text fields like names and descriptions.
Primary Key vs Unique Key
Answer
Example: Think of a student directory where every single student must have a unique ID card number that can never be left blank, alongside an optional field for a personal phone number that cannot be duplicated.
A Primary Key uniquely identifies a specific row in a table. A table can have only one primary key, and it strictly forbids any NULL or empty values from being entered.
A Unique Key ensures that values in a column are never repeated, but it typically allows a NULL value to be left blank depending on your specific database rules. You can place multiple unique keys across a single table.
Interview Tip: A primary key is the master identifier for a row, while a unique key is a secondary constraint used to stop duplicate data from creeping into other columns.
Primary Key vs Foreign Key
Answer
Imagine a company database where every employee has a unique Employee ID number, and their profile also includes a Department ID number that points back to a master department table.
A Primary Key is the master column that uniquely identifies each individual record inside its own table, ensuring that no two rows are ever identical.
A Foreign Key is a bridge column that points directly to a primary key residing in a completely different table. It acts as a constraint to maintain relational integrity between the two files.
Interview Tip: Primary keys are used to establish identity within a table, whereas foreign keys are used to link related tables together.
Candidate Key vs Primary Key
Answer
Example: Imagine an employee table where both the Employee ID number and the Social Security Number are perfectly capable of uniquely identifying a single person.
Candidate Keys represent the entire pool of columns or combinations that qualify to uniquely identify a row. In this case, both ID and SSN are candidate keys.
The Primary Key is the single, chosen candidate key that the database designer selects to act as the official, permanent master identifier for the table.
Interview Tip: Every primary key is a candidate key, but only one lucky candidate key gets picked to be the official primary key.
Natural Key vs Surrogate Key
Answer
Imagine identifying books in a store. You can either use the existing real-world barcode printed on the back cover, or print your own custom sequential serial number tag to stick on the inside jacket.
A Natural Key is built out of pre-existing real-world data that already holds a unique business meaning outside of the database, such as an email address or a Social Security Number.
A Surrogate Key is a completely artificial, automated identifier generated by the database system itself. It has no real-world meaning, usually taking the form of a simple auto-incrementing integer.
Interview Tip: Natural keys reflect real business logic, but surrogate keys are widely preferred because they keep database relationships stable even if external business data changes.
INNER JOIN vs LEFT JOIN
Answer
Imagine matching a list of students with a list of library cards. Some students do not have cards, and some cards do not belong to active students.
An INNER JOIN acts as a strict filter. It only returns records where a successful match is found across both tables. If a student does not have a library card, they are completely left out of the results.
A LEFT JOIN is loyal to the first table. It returns every single row from the left table, regardless of whether a match exists. If a student lacks a card, their name still appears, and the library card columns simply show up as NULL.
Interview Tip: Reach for an INNER JOIN when you only care about perfect matches, and use a LEFT JOIN when you need to see everything from your main table along with optional matching data.
LEFT JOIN vs RIGHT JOIN
Answer
Example: Think about comparing a table of customers on the left with a table of orders on the right.
A LEFT JOIN pulls all records from the left table and pulls matching data from the right table.
A RIGHT JOIN reverses that focus, pulling all records from the right table and matching data from the left table.
Interview Tip: Both joins do the exact same functional work if you simply flip the order of the tables in your query text. Most developers stick to LEFT JOIN because it reads more naturally from left to right.
INNER JOIN vs OUTER JOIN
Answer
Imagine looking at two overlapping circles of data. You can either extract only the sweet spot where they intersect, or pull the intersection along with the outer edges.
An INNER JOIN focuses exclusively on the center intersection, returning rows only when there is a clean match found on both sides.
An OUTER JOIN expands the scope to include the unmatched outer edges as well. It comes in three flavors: LEFT OUTER, RIGHT OUTER, and FULL OUTER, which grabs everything from both sides.
Interview Tip: Use an INNER JOIN for clean, matched datasets, and use an OUTER JOIN when you intentionally want to include missing or unmatched records.
CROSS JOIN vs SELF JOIN
Answer
Example: Think about matching a list of clothing items. You can either mix every shirt with every pair of pants to find all possible outfits, or look at an employee list to match workers with their managers.
A CROSS JOIN creates a mathematical grid called a Cartesian product. It pairs every single row from the first table with every single row from the second table, creating massive combinations.
A SELF JOIN is a regular join tactic where a table is joined back to a copy of itself using aliases. It is used to compare rows that reside within that same single table.
Interview Tip: CROSS JOIN generates every theoretical combination possible, while a SELF JOIN helps you map internal relationships within a single dataset.
SELF JOIN vs Recursive CTE
Answer
Imagine exploring a corporate hierarchy. You can either write a quick query to match employees directly to their immediate managers, or trace a deep family tree all the way up to the CEO.
A SELF JOIN connects a table to itself and is perfectly suited for simple, single-level relationships where you only need to jump up one step from a child row to a parent row.
A Recursive CTE is an advanced query expression that loops over the data repeatedly to traverse deep, multi-level hierarchies like folder structures or complex organizational charts.
Example:
- A SELF JOIN shows: Employee maps to Manager.
- A Recursive CTE traces: Employee maps to Manager, who maps to Director, who maps to the CEO.
Interview Tip: Rely on a basic SELF JOIN for simple pairs, and build a Recursive CTE when you need to climb up or down deep, multi-layered hierarchies.
Premium Content
Unlock Comparison Scenarios - Part 1 and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans