How to Write SQL Queries | Simple SELECT Statement

SELECT * Returns all Columns

How to Write SQL Queries | Simple SELECT Statement

In the previous post, we introduced some basic database fundamentals. We spoke about tables, columns, relationships and foreign and primary keys.

So continuing in my series on writing SQL queries which as I said in the first post, more and more job roles are requiring access to the database so they can slice and dice and extract value from their businesses data. In this post, we will look at writing our first query to retrieve data from a database.

This can seem a bit daunting to begin with but don’t worry, I would recommend reading the first post before this one, as we will start at the beginning and build from there post by post In this series.

What you need to follow along

This series will use the following

All of the above are free to download and install. You might need a reasonable specification laptop to run SQL Server but I suspect most commodity hardware could cope these days

We will not discuss installing these tools in any detail.

 The Simple SELECT Statement

In the previous post, we introduced SQL and what it is in the remainder of the series we will focus on retrieving data. To retrieve data from a database you need to write a query and that query will use the SELECT command.

The SELECT statement retrieves data from database tables, well what it actually does is describe or define the result set with a table structure and contents. As normal, this is best explained with an example.

Our queries can be run in SQL Server Management Studio against the AdventureWorksLT2019 database.


USE AdventureWorksLT2019;


SELECT FirstName, LastName
FROM [SalesLT].[Customer]

What this query is saying is return the FirstName and LastName columns from the SalesLT.Customer table. Notice there is no instructions on how to retrieve the data, we are just describing the result set. The result of the query is as follows

Simple Query Results

SELECT Statement Overview

The following shows the order of the various clauses used in a SELECT query. We will work our way through these clauses in subsequent posts and no doubt we will be referring back to this as we make our way on our writing SQL queries journey. The SELECT clause is always needed with at least one column expression. Just this doesn’t mean an actual table column, it could be a constant. You will see other examples of this in future posts

SELECT <Column>,...,<Column> | *
FROM <table> [JOIN <table> ON <join condition>]
WHERE <filter condition>
GROUP BY <column>,...,<column>
HAVING <filter condition>
ORDER BY <column>,...,<column>

The star or asterisks  *  all the table’s columns. Also, note you can be very specific columns names in the select statement. simply list the columns you want to be included in a comma-separated list.

SELECT * Example

The following query will return all columns and all rows from the SalesLT.Customer table

SELECT *
FROM [SalesLT].[Customer]
SELECT * Returns all Columns

How to Write SQL Queries | Simple SELECT Statement – A warning

As you can see it’s pretty simple to get started, I think it’s worth pointing out that some of the queries that we have shown in this post when run against a production system and large database with lots of rows have the potential to cause poor performance. As we work through the series hopefully we will hone our SQL writing skills to write efficient queries that both return useful results and perform well. Hopefully, not causing your DBA too much pain in the process

Other Useful Information

If you are interested in attending some formal training the check out our Writing SQL Queries training page. If you would like to speak to us directly please use this contact form

Useful Links

Check out our data analysis videos on YouTube

Check out our database fundamentals post in this How to Write SQL Queries series

 

How Do I Encrypt my SQL Server Connections?

SQL Server Consulting

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *