Back to articles list Articles Cookbook
7 minutes read

How to Start Coding SQL in VS Code

Looking to enhance your SQL development? Our guide covers everything you need to get started with SQL in Visual Studio Code, the world's most popular IDE. Discover setup tips, extensions, database connections, and more to streamline your SQL coding experience.

Visual Studio Code (VS Code) is currently the world’s most popular integrated development environment (IDE). It supports a variety of programming languages, and its massive community creates a lot of different extensions that help streamline your work or unlock even more potential within the IDE.

Today, we will take a look at how to set up VS Code to start coding SQL. Of course there are other IDEs well-suited for SQL development, but with VS Code’s popularity and customizability, it easily rises up as a top choice. We will go through everything – from installation and setting up some useful SQL development plugins to working with your favorite database engine.

Before we start, I’d like to recommend our interactive SQL from A to Z track if you’re looking for a structured environment to hone your SQL skills. This track provides a comprehensive curriculum that guides you from basic to advanced SQL concepts.

Installing VS Code

VS Code is incredibly lightweight, coming up at under 200MB for the download. You can get it for free from the official website with no strings attached. VS Code is supported on all major operating systems, and chances are, you will not have any trouble installing it. Let’s take a quick look at how to install VS Code on all currently supported operating systems.

Installing VS Code for Windows

Download either the user or the system installer, depending on if you want VS Code to be accessible to all users on the system. Run the installer and follow the instructions. I recommend allowing the “Open with Code” options in context menus; it will let you quickly open any folder from the explorer using VS Code.

Installing VS Code for MacOS

Download the archive and unzip it. Drag the .app file to the Applications folder to make it available in the Launchpad. From there, you can double click the VS Code icon to launch it.

Installing VS Code for Linux

Installing VS Code on Linux depends on which distribution you have. Here’s the general idea:

  1. Download the package file.
  2. Install the package:
    • Debian-based distributions (including Ubuntu):
      sudo apt install <path to package>.deb
    • Red Hat, Fedora, SUSE and other RPM-based systems:
      rpm -ivh <path to package>.rpm

There are many more options for installing VS Code on Linux. You can see the full list of supported systems and packages on the official documentation page.

Configuring VS Code for SQL

Enabling Syntax Highlighting

To start coding SQL using VS Code, create a new file from the menu (File->New Text File) or with the shortcut Ctrl+N. Save the file with the .sql extension so that VS Code can enable syntax highlighting for SQL.

How to Start Coding SQL in VS Code

But this is not a full development experience, so let’s boost it a bit!

Installing the SQLTools Extension

To make it easier to work with databases (and expand the basic functionality of VS Code) we are going to install the SQLTools extension. You can find it by using the search in the extensions tab (the blocks icon or Ctrl+Shift+X):

How to Start Coding SQL in VS Code

Search for “SQLTools” and install the extension we’ve highlighted in the image above. This is the main extension that will enable you to communicate with the databases.

We’ll also install database drivers for all the different database engines that you might be using.

Click the blue Install button, then scroll down to the Official Drivers list. Here you can click on relevant Marketplace links to install related drivers for any of the databases that you use. You don’t have to install every single driver, only the ones for the databases that you will use. You can always install a new driver later with just a couple of clicks.

How to Start Coding SQL in VS Code

There are also a lot of unofficial database drivers that might be a bit harder to install. But in most cases, the official drivers will meet your needs.

SQLTools is an all-in-one toolkit for SQL development. It provides database communication, syntax highlighting, IntelliSense (code autocomplete), automatic formatting, and a lot more. Let’s take a look at its biggest feature – database communication.

Connecting to a database and running queries

Once you have the extension installed and set up, the “SQLTools” tab should appear in your sidebar, just below the extensions.

How to Start Coding SQL in VS Code

If it doesn’t, restart VS Code. You can reinstall the extension if reloading it does not work.

Setting Up the Database Connection

Open the SQLTools tab and click Add New Connection. There will be a screen prompting you to select a database driver. (If you have not downloaded the driver in the previous step, or if you need a different driver, click Get more drivers at the bottom of the section.)

How to Start Coding SQL in VS Code

Use the interactive form to fill out the database connection info and credentials. Here, I will demonstrate how to set up a connection using a PostgreSQL database:

How to Start Coding SQL in VS Code

You will need:

  • Connection info:
    • The Address of the server (localhost if you are running the database on your own computer).
    • The Port on which the server is running (e.g. for PostgreSQL, it is 5432 by default).
    • The Database name to which you are going to connect, as there can be multiple databases running on the same server.
  • Database credentials:
    • The Username and Password. If your database is not protected by a password, select Use empty password from the drop-down menu. The SQLTools Driver Credentials and Save as plaintext in settings options allow you to enter the password once (during first connection and during setup, respectively) and have it saved for all future connections to the same database. The Ask on connect option will prompt you for the password every time you try to connect.

Once you have entered all the data, click the Test Connection button in the bottom right of the screen. If the connection can be established, you will get a green label saying “Successfully connected!”. In this case, you can save the connection and start using it.

If the connection cannot be properly established, you will receive an error with a short description of what went wrong. In that case, check that you have correctly entered the username and the password; if that does not fix it, ask your database administrator for help.

Writing and Running Queries

After connecting to the database, you will be able to run any .sql file in your editor using an active connection. Keep our Basic SQL Cheat Sheet handy for this.

An empty .session.sql file will be created once you connect to your database. You can write queries in this or any other .sql file and use the Run on active connection button to execute the queries using the database.

How to Start Coding SQL in VS Code

If you don’t already have a database full of data, take a look at our article on where to find practice datasets.

Every time you execute a query, a new window will appear beside the .sql file displaying the result of the query. There, you have a table with the results and some useful buttons. Clicking the Console button below the query result will show you details about the execution of the query. Re-Run Query allows you to quickly update the result of the query, if you expect the data in the database to change. The Export and Open options allow you to translate and interact with the query results using the JSON or CSV formats. Use Export to generate a new file with the result or to copy the result to clipboard; Open will open the result directly in your default application for JSON and CSV files.

Closing the Connection

To close the database connection, go to the SQLTools tab and press the red disconnect icon near an active connection. If the icon is green, you are already disconnected, and don’t need to do anything else.

How to Start Coding SQL in VS Code

Ready to Start Coding SQL in VS Code?

With the right setup in VS Code, you can turn SQL development into a more streamlined and efficient experience. In this article, we have discussed how to transform VS Code into a powerful SQL IDE. As you continue coding, these tools and tips will enhance your workflow, making tasks like debugging and writing complex queries smoother and more productive.

Want to expand your SQL skills? Take a look at our interactive SQL from A to Z track,  which feature 7 comprehensive courses that cover the full curriculum of modern SQL. This track takes you from the basics to advanced concepts, ensuring a deep understanding of each topic through real-world examples and interactive exercises. Whether you're starting out or looking to refine your expertise, this track provides all the tools and support you need to master SQL and enhance your career prospects. Dive in today and become proficient in highly valuable data skills!