How To Write SQL Queries

SQL or Structured Query Language is a specialized computer language used in data processing and data manipulation. 

Data driven analytics is one of the most important tools used in a number of industries. With this in mind, it’s vital that data can be collected and analyzed from any given database quickly and effectively. That’s where SQL comes in. In particular, SQL queries allow us to call data from a database. 

SQL is the ANSI and ISO standard for relational database management and retrieval. There are numerous database products that support SQL, including those from Oracle and Microsoft. 

Within a distributed database, the database’s “back end” runs constantly on a server. This “back end” interprets data files on said server. A client computer that has a specific program allows users to call on and manipulate the data by using tables, columns, rows, and fields. 

To do this, these programs send SQL statements to the server. These statements are then interpreted by the server and result sets are returned to the client program. 

In this article, we’ll be taking you through how to write SQL queries. We’ll also be looking at a few examples in order to demonstrate what a SQL query looks like. It may seem daunting at first, but trust us, it becomes very simple once you get the hang of it. 

How To Write SQL Queries

Understanding The Hierarchy Of A Database 

Before we get into the finer details of how to write SQL queries, it’s essential that we set a context for this guide. 

First off, it’s vital that you get accustomed to the database you’re using and its structure or hierarchy. 

You might be working with a number of databases, all with different sets of data. In this instance, you’ll want to zoom in on a specific location within the database, to find the data you want to work on. 

In very basic terms, a database with a hierarchical structure is a data model which sees data stored in the form of records which are organized into a tree-like structure.

Within this tree-like structure, one main branch can have multiple branches coming off it, and the leaves represent the raw data. 

How To Write SQL Queries: Step By Step 

To start a SQL query, you’ll need to open with a select statement. A select statement is used to query the database and highlights data that matches your criteria. 

The next step is to add all the field names you want to display. Field names are connected to particular fields which contain the data. 

After you’ve inputted the field names, you need to add statement clause(s) or selection criteria. The “from” clause is required to identify the table(s) that contain the data. 

There are a number of optional clauses which can be used to narrow down your search. Whilst the “from” clause is essential, these clauses are not: 

“Where” – Specifies selection conditions by which the data is called.

“Order By” – Denotes the ordering and sorting of rows of retrieved data.

“Group By” – This groups the results in sets. 

“Having” – Allows a specific search condition and is used with the “Group By” clause. 

With all of the above, your SQL Query should look like the following: 

Select [all | distinct] field 1 [field 2] > From table 1 [table 2] > [Where conditions] > [Group By field-list] > [Having conditions] > [Order By field-list]

How To Write SQL Queries: An In Depth Example

In this example, we’ll discuss how to retrieve data from a database that holds information about customers of a large construction company. You might need to answer a set of questions regarding orders for house extensions.

As this is a more in depth example, there are a few pieces of syntax which need to be included in the SQL query statement. Below is a blank example, with this syntax included:

SELECT column 1, column 2 FROM table1, table 2 WHERE column2= ‘value’ ;

As you can see, at the end of the statement, we’ve included a semicolon (;). In SQL, a semicolon is a statement terminator. It’s considered best practice to include a semicolon, but you’ll need to include it if you’re sending multiple statements.

It’s important to note that SQL isn’t case-sensitive. This means that in SQL, a SELECT statement is the same as a select statement. In terms of readability, most programmers use uppercase for clauses or commands, and lowercase for everything else. 

Your boss has asked you to pull data from the server regarding all customers who had housing extensions with the last name “Williamson.”

In this instance, you would need to write a SELECT statement which will give you all the customers whose last name is Williamson. 

This statement would look something like this: 

SELECT * FROM Customers WHERE Last_Name = ‘Williamson’ ;

If your boss then said that he needed a specific customer, but only knew the last part of the customer’s name, you would be able to find all customers with similar names by using the pattern-matching operator LIKE. 

If your boss said that the customer’s first name ended in “mes” here is how you would find this customer. 

In the example below, the operator LIKE is represented by a % (percentage symbol). As you only know part of the customer’s first name, the LIKE operator uses the % wild card to match zero or more characters specified:  

SELECT * FROM Customer WHERE First_Name LIKE ‘%mes%’ ;

Final Thoughts

Whilst the examples given in this article are very basic, you should now have a firm grasp on the fundamentals of writing SQL queries. 

There is so much more you can do with SQL, but having a strong understanding of the foundation will enable you to explore SQL more effectively. 

Albert Niall
Latest posts by Albert Niall (see all)