Showing posts with label sql server. Show all posts
Showing posts with label sql server. Show all posts

Tuesday, October 13, 2026

Joins in RDBMS (SQL Server)

 In SQL Server, joins let you combine rows from two or more tables based on related columns. Here’s a clear breakdown with examples:

🔑 Types of Joins in SQL Server

  1. INNER JOIN Returns rows where there is a match in both tables.

    sql
    SELECT e.EmployeeID, e.Name, d.DepartmentName
    FROM Employees e
    INNER JOIN Departments d
    ON e.DepartmentID = d.DepartmentID;
    

    👉 Only employees who belong to a department will be shown.

  1. LEFT JOIN (LEFT OUTER JOIN) Returns all rows from the left table, plus matching rows from the right table. If no match, NULLs are returned.

    sql
    SELECT e.EmployeeID, e.Name, d.DepartmentName
    FROM Employees e
    LEFT JOIN Departments d
    ON e.DepartmentID = d.DepartmentID;
    

    👉 Shows all employees, even those not assigned to a department.

  1. RIGHT JOIN (RIGHT OUTER JOIN) Returns all rows from the right table, plus matching rows from the left table.

    sql
    SELECT e.EmployeeID, e.Name, d.DepartmentName
    FROM Employees e
    RIGHT JOIN Departments d
    ON e.DepartmentID = d.DepartmentID;
    

    👉 Shows all departments, even if no employees are assigned.

  1. FULL JOIN (FULL OUTER JOIN) Returns all rows when there is a match in either table. Non-matching rows will have NULLs.

    sql
    SELECT e.EmployeeID, e.Name, d.DepartmentName
    FROM Employees e
    FULL JOIN Departments d
    ON e.DepartmentID = d.DepartmentID;
    

    👉 Shows all employees and all departments, including unmatched ones.

  1. CROSS JOIN Produces a Cartesian product (every row from one table combined with every row from the other).

    sql
    SELECT e.Name, d.DepartmentName
    FROM Employees e
    CROSS JOIN Departments d;
    

    👉 Useful for generating combinations, but can explode in size.

📌 Example Tables

Employees Table

EmployeeIDNameDepartmentID
1Alice10
2Bob20
3CarolNULL

Departments Table

DepartmentIDDepartmentName
10HR
20IT
30Finance

✅ Example Results

  • INNER JOIN → Alice (HR), Bob (IT)

  • LEFT JOIN → Alice (HR), Bob (IT), Carol (NULL)

  • RIGHT JOIN → Alice (HR), Bob (IT), Finance (NULL)

  • FULL JOIN → Alice (HR), Bob (IT), Carol (NULL), Finance (NULL)

Visual Explanation

Here’s the visual you asked for — the diagrams are ready now! They show SQL Server joins as Venn-style illustrations, making it easy to see how INNER, LEFT, RIGHT, and FULL OUTER joins work.

This way, you can quickly connect the SQL code examples we discussed with the conceptual picture of how rows are included or excluded.

Would you like me to also expand this with a real-world business scenario (like employees and departments, or customers and orders) so you can see how these joins play out in practice?



Sunday, July 30, 2023

T-SQL LEAD LAG and SUM function based query

 

Query on T-SQL window clause

Below is the sales table

Order_Date

Name

Product

SubCategory

Category

Quantity

SalesAmount

DiscountAmount

10-08-2011

Megan Stewart

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

14-09-2011

Elizabeth Clark

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

14-10-2011

Olivia Torres

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

20-10-2011

Ethan Foster

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

14-11-2011

Beth Gutierrez

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

14-11-2011

Dawn Wu

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

20-12-2011

Desiree Dominguez

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

22-12-2011

Kevin Wright

Mountain-100 Silver, 38

Mountain Bikes

Bikes

1

3399.99

0

17-01-2011

Clarence Rai

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

23-01-2011

Elizabeth Johnson

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

03-02-2011

Jaclyn Lu

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

19-03-2011

Jon Zhou

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

04-04-2011

Arthur Carlson

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

07-04-2011

Adam Ross

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

17-04-2011

Carolyn Navarro

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

29-04-2011

Abby Sai

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

05-05-2011

Ross Jordan

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

06-05-2011

April Deng

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

02-06-2011

Ebony Gutierrez

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

08-06-2011

Julian Ross

Mountain-100 Silver, 42

Mountain Bikes

Bikes

1

3399.99

0

 test


  • From the above table display all records along with sum of sales amount for each category.
  • From the above table display all the records along with sales amount per month.
  • Compare the sales between each day with previous sales value for each category.

pandas vs pyspark dataframe

  📊 Pandas vs Spark DataFrame Aspect Pandas Spark DataFrame (PySpark) Execution Model Eager (operations run immediately) Lazy (builds DAG, ...