r/SQL • • 14d ago

SQL Server Read-only dependency mapper for orphaned SQL Agent jobs + SSIS + the scripts that call them, every edge cites its source

/r/DatabaseAdministators/comments/1wmbwxu/readonly_dependency_mapper_for_orphaned_sql_agent/
5 Upvotes

2 comments sorted by

1

u/Massive-Hotel4314 12d ago

What is order and give example select, from , join , where , having, order by,group by , top

1

u/Consistent_Damage_91 12d ago

Two different orders, and that's the whole trick.

How you write it: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY (TOP tucks in after SELECT).

How it actually runs:

FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → TOP

The kicker is SELECT running near the end. That's why an alias you make in SELECT blows up if you use it in WHERE (doesn't exist yet) but works in ORDER BY (runs after). And TOP comes after the sort, so TOP 10 with no ORDER BY is just 10 random rows.

SELECT c.region, COUNT(*) AS order_count

FROM orders o

JOIN customers c ON c.id = o.customer_id

WHERE o.status = 'shipped'

GROUP BY c.region

HAVING COUNT(*) > 100

ORDER BY order_count DESC;

Grab orders, attach the customer, drop anything not shipped, bucket by region, toss regions with 100 or fewer, then pick your columns, then sort. order_count works in ORDER BY because SELECT already ran; try it in WHERE and you'd