Querying Archives

A majority of SQL focuses on querying data.  Makes sense, as that is one of the main purposes of a database!

In this section we’ve collected several categories related to querying, namely:

  • Sorting – Ordering rows by columns and expressions and selecting the top or bottom results.
  • Filtering – The ability to use Boolean logic to select one set of rows with a result over another.
  • Group and Summarize – Use GROUP BY and HAVING to calculate summary data such as averages, summations, minimum and maximum values.

If you’re new to SQL, I would recommend you join our Essential SQL learning group on FB.

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the sample databases AdventureWorks and WideWorldImporters.  You can get started using these free tools using my guide Getting Started Using SQL Server

How to use the BETWEEN operator to compare a range of values in SQL SERVER Queries

In this video we’ll walk you though how to use the BETWEEN operator; we’ll show you why you would want to use the BETWEEN operator and how to avoid pitfalls when using it with the DATETIME type. The BETWEEN operator is equivalent to using AND combination of >= and <= comparison operators, but makes for […]

Continue reading

Calculate a Running Total in SQL with Joins or Window Functions

There are several ways to calculate a running total in SQL.  In this article, we will cover two methods:  Joins, and Window Functions. We’ll first look at how to calculate the running total using an INNER JOIN.  By doing so, you’ll not only learn more about join conditions, but see how to take the result […]

Continue reading
3

How do the SQL DISTINCT and TOP SELECT Clauses Work Together to Generate Results?

Distinct pattern of terminals

This article is inspired by a series of questions that one of my readers, Nan, recently sent me regarding DISTINCT, TOP, and ORDER BY. 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 my Guide Getting Started Using […]

Continue reading
3

What is the difference between WHERE and HAVING clauses?

In this article learn when to use WHERE and HAVING.  Both perform similar functions, but for different purposes! All the examples for this article are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools using my Guide Getting Started Using SQL Server. How do Where and Having Differ? […]

Continue reading
2

What is the Difference between ORDER and GROUP BY?

Differences in Order By versus Group By

The GROUP and ORDER BY clauses are both used to organize data.  Learn which is used to sort the data, and which is used for counting and summing. 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
9

Use SQL Server to Sort Alphanumeric Values

Sort Alphanumeric Values with SQL Server Recently a reader asked me how to sort an alpha numeric field in SQL in natural order.  Alphanumeric values are commonly found and don’t sort naturally using numeric methods. When we have a numeric fields we expect values to be sorted in ordinal order, for example: 1,2,5,7,10,11,15,20,21 However when […]

Continue reading
4

How to Use GROUP BY to Summarize Query Results

Group and Summarize Data

In today’s lesson you’re going to learn about grouping results returned from your queries using the GROUP BY clause. The objectives of today’s lesson are to: Learn how to group results using GROUP BY Use aggregate functions to perform calculations Understand how to filter groups using the HAVING clause Important! Please follow along and do the […]

Continue reading
2

Use Pattern Matching in SQL to Filter Results

In this lesson you are going to explore how to use pattern matching in SQL with the LIKE operator.  Using this phrase allows us perform partial matches of data values and obtain answers to questions which can’t be done with conventional comparisons. The lesson’s objectives are to: learn about the LIKE match condition understand wild cards Important! Please follow […]

Continue reading
3

Get Ready to Learn SQL Server:  4. Query Results Using Boolean Logic

In today’s lesson you’re going to learn more about filtering results returned from your queries using the WHERE clause. The objectives of today’s lesson are to: Learn to use more than one compare condition at a time using Boolean logic Comprehensive example with Select, Where, and Order By Important! Please follow along and do the examples […]

Continue reading
5

How to Filter Query Results

In today’s lesson you’re going to learn how to filter query results using the WHERE clause.  This clause is important as only those records matching the where clause’s conditions are returned in the query results. The objectives of today’s lesson are to: Learn about various condition types, such as Equality, Range, and Membership Comprehensive example […]

Continue reading
9

Getting Started With SQL Server: 2. Sort Your Query Results

In this lesson you are going to explore how to sort your query results by using SQL’s ORDER BY statement.  Using this phrase allows us to sort our result in ascending or descending order.  In addition you can limit your query to a specified number of results. The lesson’s objectives are to: learn how to sort on […]

Continue reading
8

Getting Started with SQL Server: 1. Simple SELECT Queries

In this series of lessons you’re going to learn how to query Microsoft SQL Server using the SQL SELECT statement.  Once you have read this lesson you’ll be able to: Identify all the tables in a SQL Server Database Using SSMS (SQL Server Management Studio). Understand how to list a table’s columns. Use the SQL SELECT statement to view […]

Continue reading