Tag Archive

Tag Archives for " subquery "

Joins versus Subqueries SQL Puzzle

Joins versus Subqueries

In this puzzle, we’re going to learn about joins versus subqueries.  In many queries you can substitute joins and subqueries.  Yet, since each has their strengths, it isn’t wise to do so.  Once you have worked through this puzzle you see some of the benefits and disadvantages of joins versus subqueries. Solving puzzles is a […]

Continue reading

What is the difference between a subquery and inner join?

In this puzzle, we’re going to learn how to rewrite a subquery using inner joins.  Knowing about a subquery versus inner join can help you with interview questions and performance issues.  Though subqueries have unique abilities, there are times when it is better to use other SQL constructs such as joins. By reading this article […]

Continue reading

Learn to use the Data Dictionary in SQL Server

In this puzzle, we’re going to learn how to query the data dictionary using SQL Server.  Knowing how to query the data dictionary is good to know.  There are many questions you can answer about your database through the data dictionary. For instance, do you know how many of your table don’t have a primary […]

Continue reading

Work with the AdventureWorks Bill of Materials using Subqueries

In this puzzle, we’re going to learn how to work with the AdventureWorks Bill of Materials table.  Companies use a BOM (Bill of Materials) to itemize the products that are used or contained in another product. Solving puzzles is a great way to learn SQL.  Nothing beats practicing what you’ve learned.  Once you have figured […]

Continue reading
1

Rank Data with SQL

In this puzzle, we’re going to work through a problem to identify the top and bottom 25 percent to rank student scores. Solving puzzles is a great way to learn SQL.  Nothing beats practicing what you’ve learned. SQL Puzzle Question In today’s puzzle assume you’ve been asked by the head of the Mathematics department to […]

Continue reading

Recursive CTEs Explained

Recursive CTE’s In this article we explore recursive CTE’s (Common Table Expressions).  Recursive CTEs are special in the sense they are allowed to reference themselves!  Because of this special ability, you can use recursive CTEs to solve problems other queries cannot.  Recursive CTEs are really good at working with hierarchical data such as org charts […]

Continue reading

Non Recursive CTEs Explained and Why to Use Them

Introduction to Non Recursive CTEs In this article we explore non recursive CTEs (Common Table Expressions).  This is a broad class, and basically covers every form of CTEs except those that call themselves.  This other class is called the recursive CTEs; they are covered in the next article. If you’re unfamiliar with CTEs I would […]

Continue reading
2

Is it possible to use Joins, Subqueries, and Unions in one Query?

A reader recently asked me to help them solve a problem combining data from multiple tables into a single result.  What was interesting was that the solution used the three ways to combine data that we have recently covered, namely:  JOINS, set operators, and subqueries. Given the comprehensive nature of this problem I thought it […]

Continue reading

Get Ready to Learn SQL Server 23: Using Subqueries in the HAVING Clause

HAVING Clause Subquery

This is the fifth in a series of articles about subqueries.  In this article we discuss subqueries in the HAVING clause.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools using […]

Continue reading
2

Using A Subquery in the FROM clause

Derived Tables in From Statement

This is the fourth in a series of articles about subqueries.  In this article we discuss using a subquery in the FROM clause.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools […]

Continue reading

Using Subqueries in the WHERE Clause

Sub query in Where Clause

This is the third in a series of articles about subqueries.  In this article we discuss subqueries in the WHERE clause.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools […]

Continue reading
8

Using Subqueries in the Select Statement

Subqueries in Select Statement

This is the second in a series of articles about subqueries.  In this article we discuss subqueries in the SELECT statement’s column list.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these […]

Continue reading
4

Learn SQL – Introduction to Subqueries

Introduction to Sub Queries

The purpose of this article is to introduce you to subqueries and some of their high-level concepts.  There are a lot of details to cover in order to learn sub queries, but you’ll see we cover those in depth in later articles. All the examples for this lesson are based on Microsoft SQL Server Management […]

Continue reading
2

How do I combine results from more than one table?

Combine data in SQL

Many times in a relational database the information you want to show in your query is in more than one table.  This begs the question “How do you combine results from more than one table?” All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can […]

Continue reading