Back to articles list Articles Cookbook
5 minutes read

How to Begin Running SQL Queries

In this article, I’ll explain how to run a SQL query to help you execute your first one. Let’s jump right in!

Running SQL queries for the first time is not a complex task, but it can seem intimidating at first if you’re a complete beginner. But once you get past that initial roadblock, you’ll be able to focus on learning SQL and writing more interesting queries to meet your business needs.

Running SQL Queries: The Ingredients

You’ll need these three things for running SQL queries:

  • A database engine
  • An SQL client
  • An SQL query

What is a Database Engine?

A database engine is like a data repository; it contains all the data (tables, procedures, and more) related to your organization, business, or personal databases. Installing a database engine is fairly easy, and it’s the first step you should take to be able to run SQL queries on your computer.

There are many different database engines on the market, such as Oracle, SQL Server, DB2, MySQL, and PostgreSQL. Some of these engines are developed by companies for commercial or limited personal use. For example, Microsoft developed SQL Server, while Oracle developed the Oracle and MySQL engines.

Among the engines on this list, PostgreSQL is a special case because it is an open-source database engine. That means it’s developed by a community of developers, and anyone can use it for free (including you!).

It’s important to understand that all database engines speak the core SQL language. So if you learn SQL while working in Oracle, you can easily change gears and work with SQL Server, PostgreSQL, or any other database engine that uses standard SQL or a derivative of it.

Getting Started with SQL Queries: Choosing a Database Engine

To start, you need to identify what database engine you’d like to use. You’ll also need some additional technical information, like the names of the tables where the data you want to work with will be stored. In SQL, data is organized and grouped in tables. A database engine consists of several databases, which in turn consists of tables that store data and procedures that manipulate data.

As an example, consider the following two independent database engines, one for a company named GoGreen that runs in Oracle and another for a sports retail company named BeOlympic that uses PostgreSQL.

Small companies tend to use free database engines like PostgreSQL or MySQL, while medium-sized companies and corporations tend to use more robust database engines like Oracle, DB2, or SQL Server. However, it’s important to note that these days, almost all database companies offer free versions of their databases, perhaps with some limitations, oriented towards small businesses.

The SQL Client: Your Connection to the Database Engine

This step can take some time, but it’s not too difficult, and once you install an SQL client, you won’t need to do so again (unless you decide to work with another database engine).

Once you’ve downloaded your selected database engine, you’ll need to download an SQL client to be able to communicate with that engine. Fortunately, in some cases the SQL clients come pre-installed with database engine installations, in other cases you need to download, install and configure it. For example, on Windows the pgAdmin client is part of the PostgreSQL database engine installation; it’s an interactive interface that allows you to create your own databases, add tables, write queries, and do all kinds of neat things. Take a look at the “How to Install PostgreSQL on Windows in 5 Minutes” article to understand installation and configuration of Postgres engine and pgAdmin SQL client.

SELECT product_id, description, cost, price FROM products 

Here are the results of the query:

Writing an SQL Query: the Final Piece of the Puzzle

At last, we’ve arrived at the promised land. Having installed a database engine and an appropriate SQL client, you’re now ready to learn how to run a SQL query. This is the simplest query you could write:

SELECT * FROM products 

For the sports retailer database, we’d write:

SELECT * FROM clients.

Once you nail down the basics, you’ll learn how to write and run a SQL query of a higher level by adding new clauses, like the WHERE clause to filter records.

Another important aspect of the SQL language is PL-SQL (PL stands for Programing Language), which is a language for writing procedures or functions that you can later execute on the data in your database. These procedures use variables, conditional statements, control statements, and lots of other programming concepts.

Consider the example below. This SQL query obtains all products with a price range of 10.00 to 20.00 dollars, inclusive.

Learn More about Running SQL Queries

Having read this article, you know how to run a SQL query. You need to:

  1. Choose a database engine for your needs and install it.
  2. Start up the database engine, and connect to it using your SQL client.
  3. Write SQL queries in the client (and even save them to your computer).
  4. Run the SQL query on your data.

SQL is a powerful language with plenty of possibilities. Even though I have years of experience working with SQL, I discover something new every week. Learning SQL is a lifelong process—start learning the basics today to begin your journey!