What is a Self Join?

When working with SQL databases, you’ll sometimes encounter scenarios where the data you need to compare or relate exists in the same table. Typical examples of this include employees who manage other employees, tasks that depend on other tasks, or categories nested within categories. These situations call for a specific querying approach called a self join.

A self join is a technique that lets you compare and relate rows within a single table. This makes them perfect for working with hierarchical data, finding relationships between records, and solving a wide range of queries that would otherwise be difficult or impossible with standard joins alone.

Read more

What is a Subquery?

A subquery is a query nested inside another SQL statement. It’s basically a query within a query. You’re using the results of one SELECT statement to help another SQL statement do its job.

Subqueries let you break down complex problems into smaller, more manageable pieces, making your SQL more readable and often more powerful. The outer query relies on the inner query (the subquery) to provide data, filter results, or perform calculations. Once the subquery executes and returns its results, the outer query uses that information to complete its task.

Read more

What is a Materialized View?

A materialized view is a database object that stores the results of a query physically on disk, rather than computing them on the fly every time you need them. It’s basically a snapshot of your query results that you can refresh periodically. Unlike regular views (which are just saved queries that execute each time you use them), materialized views pre-compute and cache the data, making subsequent reads much faster.

Read more

What is a Cursor in SQL?

If you’ve been working with SQL for a while, you’ve probably heard someone mention cursors, usually followed by a warning to avoid them. Maybe you’ve used them yourself. But what exactly are cursors, and why do they get such a bad rap? Let’s take a look at what cursors are, how they work, and when (if ever) you should actually use them.

Read more

What is a Derived Table in SQL?

If you’ve been writing SQL for a while, you’ve probably run into situations where you need to query data that’s already been aggregated, filtered, or transformed in some way. Maybe you need to calculate an average first, then find all the rows above that average. Or perhaps you need to group data by category, then filter those grouped results.

This is where derived tables can come in handy. Derived tables let you build queries in layers, where one query’s output becomes another query’s input, all within a single SQL statement.

Read more

What is a Temp Table?

Need a high-performance, disposable workspace for your SQL queries? Meet temporary tables, also known simply as temp tables. As their name implies, these tables offer a short-lived storage solution, ideal for holding intermediate data or simplifying complex multi-step processing. They exist just long enough to get the job done, providing a handy scratch pad that vanishes automatically to keep your database clean.

Read more

What is a Rollback in SQL?

In SQL, a rollback is a command that reverses all the changes made during a transaction. When you execute a ROLLBACK statement, the database management system undoes all the Data Manipulation Language (DML) operations (such as INSERT, UPDATE, and DELETE) that happened since the transaction began (or since a specified savepoint), restoring the database to its previous consistent state.

Read more

What is a Commit in SQL?

If you’ve spent any time working with databases, you’ve probably noticed that most of your SQL statements just work. You run an INSERT, UPDATE, or DELETE, and the changes happen. You don’t need to do anything special to make them stick. Your changes were committed automatically as soon as you ran the statement. No need for a separate COMMIT keyword.

But then there are other cases where you need to explicitly use a COMMIT keyword.

So why is COMMIT required in some cases and not in others?

Read more

What is Transaction Starvation?

Transaction starvation is one of those database problems that can sneak up on you when you least expect it. It happens when a transaction sits waiting for resources it needs to complete, but those resources never become available, or at least not for an unreasonably long time. The transaction essentially “starves” while other transactions keep getting priority access to the resources it needs.

Read more