Database Queries

Database Queries lay at the heart of SQL.

They are used to retrieve results from tables and views.  The columns and rows make up the results.  You can use the SELECT statement to display specific columns from tables.  In addition, you can filter rows, using the WHERE clause.

The SELECT statement consists of several main clauses:

Once you master the SELECT statement, you’ll ability write database queries and analyze data increases.

To learn more about SELECT, check out these guides:

Once you understand how these work, I would recommend you start looking at Joins and Subqueries.

Latest Posts

  • Window Functions vs Group By Queries?
    ,

    ·

    Window Functions vs Group By Queries?

    In this article let’s look at window functions vs GROUP BY queries. We’ll do so by looking at how each statement works and giving examples. Though you can use both GROUP BY and window functions to create summary values, the way SQL includes the results are different. Let’s first look at how GROUP BY does…

  • Contains in SQL

    ·

    Contains in SQL

    Sooner or later, you want to know when a column contains in SQL another value.  In this article we’ll go over several ways you can test to see whether one value is contained within an another. What makes this problem different, is that we aren’t looking for an exact match, rather, we’re looking for a…

  • SQL HAVING Tutorial
    ,

    ·

    SQL HAVING Tutorial

    Use SQL HAVING to filter summary results from a GROUP BY.  It specifies the search condition for the group or aggregate. SQL HAVING is only used with SELECT.  It is mostly used when a GROUP BY is present, if one isn’t there is an implicit single aggregated group. When constructing a SELECT statement using HAVING…

  • SQL GROUP BY Guide
    ,

    ·

    SQL GROUP BY Guide

    Use the SQL GROUP BY Clause is to consolidate like values into a single row.  The group is a set of columns. The group by returns a single row from one or more within the query having the same column values. Its main purpose is this work alongside functions, such as SUM or COUNT, and provide a…

  • What is the difference between WHERE and HAVING clauses?
    ,

    ·

    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?…

  • SQL WHERE LIKE

    ·

    SQL WHERE LIKE

    In this article you’ll learn to use the LIKE operator in your SQL. In most situations you’ll find yourself using LIKE within the WHERE clause to filter data using patterns such as all values beginning with “S.” Using this phrase allows us perform partial matches of data values and obtain answers to questions which can’t…

  • How do I use the Like Clause in SQL Server?
    ,

    ·

    How do I use the Like Clause in SQL Server?

    In this video we’ll walk you though how to use the LIKE clause.   We’ll show you how to you can perform partial matches, such as being able to match all names beginning with “B” or ending in “Y.”  We’ll also show you how to use use ranges.  For instance you may want select all…

  • SQL WHERE – Guide and Examples

    ·

    SQL WHERE – Guide and Examples

    In today’s lesson, you’re going to learn how to filter query results using the SQL 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: Important! Please follow along and do the examples in your database.  If you…

  • Use SQL ORDER BY to Sort Results

    ·

    Use SQL ORDER BY to Sort Results

    The SQL ORDER BY clause is used to sort your query result in ascending or descending order. Once you have written a query, you’ll naturally want to reorder the results. You can do so using the ORDER BY clause. SQL ORDER BY is versatile. Use the ORDER BY keyword to sort results with a SELECT statement.  You can…

  • What is the Difference between TOP and OFFSET & Fetch?
    ,

    ·

    What is the Difference between TOP and OFFSET & Fetch?

    Both TOP and OFFSET & FETCH can be used to limit the number of rows returned.  OFFSET and FETCH can return similar results to top, but there are differences which may influence which method is best for you to use in your given situation. All the examples for this lesson are based on Microsoft SQL…

  • Using OFFSET and FETCH with the ORDER BY clause

    ·

    Using OFFSET and FETCH with the ORDER BY clause

    In this article we explore the OFFSET and FETCH clauses.  OFFSET and FETCH are used in conjunction with the SELECT statement ORDER BY clause to provide a means to retrieve a range of records.  The starting row to return is determined by the OFFSET value and the maximum number of rows to return from that…

  • SQL TOP

    ·

    SQL TOP

    Some times you just need to use SQL TOP to reduce the number of rows shown in your result. This is handy for troubleshooting for showing a summary, such as the first in a list. SQL TOP Video I put together a video to help you understand TOP. If covers the main topics presented in…

  • SQL SELECT

    ·

    SQL SELECT

    You will learn how to query Microsoft SQL Server using the SQL SELECT statement in this series of lessons. Once you have read this lesson you’ll be able to: Important! Please follow the examples in your database and do them.  If you haven’t already done so, sign up for my Guide to Getting Started with…

  • SQL IN  Operator
    ,

    ·

    SQL IN Operator

    The SQL In operator compare a column to a list. In this article we’ll see how to use In and NOT IN with list generated using subqueries. SQL IN Operator and NOT IN Operator Review The SQL IN operator is considered a membership type.  The membership type allows you to conduct multiple match tests compactly…

  • Order of Execution in SQL Explained

    ·

    Order of Execution in SQL Explained

    Knowing the order of execution in SQL helps you better understand SQL’s “hair pulling” errors! As you start to work with SQL you find that some of the errors don’t make sense or you wonder why you’re able to use a column alias in the ORDER BY clause but not in a join condition. As…

  • Automatically Format Your SQL
    ,

    ·

    Automatically Format Your SQL

    Hey, welcome back to another SQL Minute. Today we’re going to learn how to code like a boss by delving into formatting your code. Now, some of you may have some rules you use to format code. Others may just type it out and wonder how they should format it. What I’m going to do…

  • An Ultimate Guide to Write an SQL Query

    ·

    An Ultimate Guide to Write an SQL Query

    The knowledge of SQL has always been in demand. Even if you don’t have a job as a data analyst or any other relevant position, knowing how to write basic SQL queries can be a useful addition to your resume. There are plenty of resources to learn about SQL, including our informative Essential SQL blog.…

  • ·

    SQL Less Than or Equal To

    The SQL Less Than Or Equal To comparison operator (<=) is used to compare two values.  It returns TRUE if the first value is less than the second.  If the second is less, it returns FALSE.  You can also test for Less than by using <.   Here are a couple of examples:  10 <=5 FALSE 5 <= 20 TRUE 10 <= 10 TRUE Here is…

  • ·

    SQL Greater Than or Equal To Comparison Operator

    The SQL Greater Than or Equal To comparison operator (>=) is used to compare two values.  It returns TRUE if the first value is greater than or equal to the second.  If the second is greater, it returns FALSE.  You can also test for greater than or equal to by using >=.   Here are a couple of examples:  10 >= 5 TRUE …

  • ·

    SQL Less Than Comparison Operator

    The SQL Less Than comparison operator (<) is used to compare two values.  It returns TRUE if the first value is less than the second.  If the second is less, it returns FALSE.  You can also test for Less than or equal to by using <=.   Here are a couple of examples:  10 < 5 FALSE 5 < 20 TRUE 10 < 10 FALSE Here…

  • ·

    SQL Greater Than Comparison Operator

    The SQL Greater Than comparison operator (>) is used to compare two values.  It returns TRUE if the first value is greater than the second.  If the second is greater, it returns FALSE.  You can also test for greater than or equal to by using >=.   Here are a couple of examples:  10 > 5 TRUE  5 > 20 FALSE 10 >…

  • ·

    SQL SELECT AS

    Rename Columns with SQL SELECT AS You can use a form of SQL SELECT AS to rename columns in your query results. So far you’ve seen where queries results return results named after the table columns. This is fine for most cases, but once you start working with expressions, you’ll see this doesn’t work well.…

  • Basic SQL:  A Business User’s Guide to Writing Queries

    ·

    Basic SQL: A Business User’s Guide to Writing Queries

    Most people are familiar enough with basic SQL to be able to provide a simple explanation of it. Unfortunately, few know how to use the language or apply it to their business goals. Many see SQL as an advanced language intended for use by data professionals and programmers. In reality, it’s a fairly simple language…

  • How to include a single quote in a SQL query
    ,

    ·

    How to include a single quote in a SQL query

    In this video we’ll walk you through how to include a single quote in a SQL query; we’ll show how to escape the single quote, so it is treated as text rather than text delimiters. Once you’ve gone through this article, I would recommend watching our next Essential SQL Minute continue to learn more about…

  • Use SQL to Calculate a Running Total
    , ,

    ·

    Use SQL to Calculate a Running Total

    The running total in SQL can be calculated in several ways. This article will cover two methods:  the Joins and the Window functions. We will first look at how to calculate the running total using the INNER JOIN.  By doing so, you’ll not only learn more about join conditions, but see how to take the result…

  • SQL DISTINCT and TOP in Same Query
    ,

    ·

    SQL DISTINCT and TOP in Same Query

    This article is inspired by a series of questions that one of my readers, Nan, recently sent me regarding SQL 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…

  • What is the Difference between ORDER and GROUP BY?
    ,

    ·

    What is the Difference between ORDER and GROUP BY?

    Both the GROUP and ORDER BY clauses are used in organizing data. Find out which of the two is used to sort the data and which is used for counting and summing up. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started…