Intermediate SQL — Joins, Subqueries, Views & Aggregations
🌟 Introduction
Welcome to Part 2 of the Oracle Live SQL Mastery Series.
In the previous part, we explored fundamentals of Oracle Live SQL, learning how to write basic queries, filter data, and perform simple joins.
Now, we’re stepping into intermediate SQL — the bridge between beginner-friendly syntax and professional database development.
By the end of this article, you’ll confidently use:
✅ JOINS (Inner, Outer, Self, Cross)
✅ Subqueries
✅ Views
✅ Grouping & Aggregations
✅ Performance tips for production-level SQL
These are the skills most developers need to pass SQL interview tests and build efficient data-driven web applications.
🧩 Understanding Joins — The Heart of Relational Databases
A JOIN combines rows from two or more tables based on related columns between them.
Let’s visualize it 👇
🔶 ER Diagram Example
📊 Relationship:
Each employee belongs to one department, but each department can have many employees — a one-to-many relationship.
🔗 1. INNER JOIN
Explanation:
-
Combines only rows that have a match in both tables.
-
Employees with no department won’t appear.
✅ Use Case: Most common join in day-to-day analytics and backend queries.
🔗 2. LEFT JOIN (LEFT OUTER JOIN)
Explanation:
-
Returns all employees, even if they don’t belong to any department.
-
Unmatched department names will be
NULL
.
🧠 Tip: Use LEFT JOIN when you don’t want to lose data from your main table.
🔗 3. RIGHT JOIN (RIGHT OUTER JOIN)
Explanation:
-
Returns all departments, even those without employees.
✅ Example: See which departments are currently unassigned to employees.
🔗 4. FULL OUTER JOIN
Explanation:
-
Returns all records from both tables, whether or not matches exist.
-
Missing data is filled with
NULL
.
🔁 5. SELF JOIN
Used when you join a table to itself — perfect for hierarchical data (like managers and employees).
📘 Explanation:
Each employee row joins with another employee record acting as a manager.
💡 Common in HR systems where employees report to other employees.
💫 6. CROSS JOIN
Explanation:
Generates a Cartesian product — every employee combined with every department.
⚠️ Warning: Avoid unless you intentionally want all combinations (e.g., generating test data).
📊 GROUP BY and HAVING
Grouping lets you summarize data, often combined with aggregate functions like SUM()
, AVG()
, and COUNT()
.
🧮 Example 1: Count Employees per Department
Explanation:
GROUP BY
clusters rows with the same department ID and counts them.
🧮 Example 2: Use HAVING to Filter Aggregates
Explanation:
-
HAVING
works likeWHERE
, but for grouped data. -
Filters groups with an average salary over 8000.
🧠 Note: You cannot use WHERE
for aggregated results — that’s why HAVING
exists.
🔍 Subqueries — Query Inside a Query
A subquery is a query nested inside another. It helps break down complex problems into smaller, modular parts.
🔹 Example 1: Find Employees Earning More Than the Average Salary
Explanation:
-
Inner query computes the average salary.
-
Outer query lists employees above that average.
🔹 Example 2: Use Subquery in FROM Clause
🧠 Explanation:
This “inline view” first calculates total salary per department, then filters departments exceeding 50,000.
🔹 Example 3: Correlated Subquery
Explanation:
-
The subquery depends on the outer query.
-
It compares each employee’s salary with their department’s average.
💡 Performance Tip: Correlated subqueries can be slower — optimize using joins where possible.
🏗️ Views — Creating Virtual Tables
A view is a stored SQL query that behaves like a table.
You can query it, join it, or even base other views on top of it.
🧱 Example: Creating and Using a View
Then you can query it like a regular table:
Benefits:
✅ Simplifies complex queries.
✅ Improves readability and security.
✅ Lets developers reuse query logic efficiently.
🧩 Updating Through Views
If your view is based on a single table (without aggregates or joins), you can update data through it:
Oracle updates the underlying employees
table automatically.
🧮 Aggregations in Real Projects
Imagine you’re building a JavaScript dashboard showing departmental salary insights.
Here’s a practical query you’d run in Oracle Live SQL:
This dataset could be sent via an API to your frontend to visualize data using Chart.js, Recharts, or D3.js.
📈 Visualizing the Flow of SQL Execution
Let’s visualize how Oracle processes your SQL step-by-step:
Understanding this execution order helps debug and optimize complex SQL.
⚙️ Query Optimization Best Practices
When working in Oracle Live SQL, even small optimizations make a huge difference:
-
Use Indexes Wisely
Helps Oracle locate rows faster.
-
**Avoid SELECT ***
Specify columns to reduce load. -
Replace Subqueries with Joins
Joins are often faster than correlated subqueries. -
Use ANALYZE and EXPLAIN PLAN
View the plan in the “Execution Plan” tab in Live SQL.
-
Use View Caching
Precompute frequent reports with materialized views.
💡 Materialized Views (Advanced Peek)
Unlike normal views, materialized views store data physically — ideal for analytics dashboards.
🧠 These can be refreshed automatically to keep analytics fast and up-to-date.
🧑💻 Practice Challenge
1️⃣ Find departments where the total salary exceeds 50,000.
2️⃣ List employees earning above their department’s average.
3️⃣ Create a view showing top 10 earners company-wide.
All can be practiced directly on Oracle Live SQL.
🧠 Interview-Focused Questions from This Module
Question Explanation What’s the difference between WHERE and HAVING? WHERE filters rows; HAVING filters groups. Can we update data through a view? Yes, if it’s based on one table without aggregates. What is a correlated subquery? A subquery referencing columns from the outer query. Difference between INNER JOIN and LEFT JOIN? INNER shows matches only; LEFT keeps unmatched rows too. What are materialized views used for? Precomputed data storage for faster reporting.
Question | Explanation |
---|---|
What’s the difference between WHERE and HAVING? | WHERE filters rows; HAVING filters groups. |
Can we update data through a view? | Yes, if it’s based on one table without aggregates. |
What is a correlated subquery? | A subquery referencing columns from the outer query. |
Difference between INNER JOIN and LEFT JOIN? | INNER shows matches only; LEFT keeps unmatched rows too. |
What are materialized views used for? | Precomputed data storage for faster reporting. |
🧾 Summary
In this part, you learned how to:
✅ Use Joins effectively (Inner, Outer, Self, Cross).
✅ Write Subqueries for complex filtering.
✅ Build Views to simplify queries.
✅ Group and aggregate data professionally.
✅ Optimize SQL for real-world performance.
✍️ By Ashish | 2025-10-21T09:01:31.424Z