LibraryExecuting SQL Queries: `Query` and `Exec`

Executing SQL Queries: `Query` and `Exec`

Learn about Executing SQL Queries: `Query` and `Exec` as part of Go Programming for Backend Systems

Executing SQL Queries in Go: Query vs. Exec

When working with databases in Go, you'll frequently need to interact with your SQL database. The

code
database/sql
package provides two primary methods for executing SQL statements:
code
Query
and
code
Exec
. Understanding the difference between them is crucial for writing efficient and correct database operations.

The `Query` Method: Retrieving Data

The

code
Query
method is used for SQL statements that are expected to return rows of data. This typically includes
code
SELECT
statements. It returns a
code
*sql.Rows
object, which is an iterator that allows you to loop through the result set.

`Query` is for `SELECT` statements that return data.

Use Query when you need to fetch records from your database. It returns a *sql.Rows object, which you'll then iterate over to access each row's columns.

The Query method takes a SQL query string and any arguments to be passed to it. It returns a *sql.Rows and an error. If an error occurs during execution, *sql.Rows will be nil. You must always call rows.Close() when you are finished with the result set to release the database connection. Iterating through the rows is done using the rows.Next() method, and you retrieve column values using rows.Scan().

The `Exec` Method: Modifying Data

The

code
Exec
method is used for SQL statements that do not return rows, but rather perform an action on the database. This includes
code
INSERT
,
code
UPDATE
,
code
DELETE
, and
code
CREATE TABLE
statements. It returns a
code
sql.Result
object, which contains information about the execution, such as the number of rows affected and the ID of the last inserted row.

`Exec` is for statements that modify data or schema.

Use Exec for operations like inserting, updating, or deleting records. It returns a sql.Result which tells you how many rows were affected.

Similar to Query, Exec takes a SQL query string and arguments. The sql.Result interface has methods like RowsAffected() to get the count of modified rows and LastInsertId() to retrieve the ID of the last inserted row (if applicable). Exec does not return *sql.Rows because no result set is expected.

Key Differences Summarized

FeatureQueryExec
PurposeRetrieve data (e.g., SELECT)Modify data/schema (e.g., INSERT, UPDATE, DELETE, CREATE)
Return Value*sql.Rows (for iterating results)sql.Result (for affected rows, last insert ID)
Data ExpectedYes, multiple rows and columnsNo, only execution status
Resource ManagementRequires rows.Close()No specific resource to close from the method itself

Always remember to close your *sql.Rows after using Query to prevent resource leaks.

Illustrative Example: Inserting and Selecting

Let's consider a common scenario: inserting a new user and then fetching all users. We'll use

code
Exec
for the insert and
code
Query
for the select.

Imagine a simple user table with id (integer, primary key) and name (text). We first use Exec to add a new user, then Query to retrieve all users. The Exec call returns a sql.Result object, from which we can get the LastInsertId. The Query call returns *sql.Rows, which we then iterate through using rows.Next() and rows.Scan() to extract the id and name for each user.

📚

Text-based content

Library pages focus on text content

Which method should you use to execute a DELETE FROM users WHERE id = ? statement?

You should use the Exec method because it modifies data and does not return rows.

What is the primary return type of the Query method in Go's database/sql package?

The primary return type is *sql.Rows.

Learning Resources

Go Database/SQL Package Documentation(documentation)

The official Go documentation for the database/sql package, detailing the `Query` and `Exec` methods and their usage.

Go SQL Tutorial: Query and Exec(blog)

A clear and practical explanation of how to use `Query` and `Exec` with code examples in Go.

Working with Databases in Go(documentation)

An overview of database access in Go, including best practices and common patterns for using the database/sql package.

Go Programming: Database Operations(tutorial)

A tutorial covering basic database operations in Go, including examples of executing SQL queries.

Go SQL: Query vs Exec(blog)

A Medium article that breaks down the differences between `Query` and `Exec` with illustrative code snippets.

Understanding Go's database/sql Package(tutorial)

A comprehensive guide to using Go's standard library for database interactions, covering query execution.

Go SQL: Handling Results and Errors(blog)

Focuses on how to effectively handle the results returned by `Query` and `Exec`, including error management.

SQL Injection Prevention in Go(documentation)

While not directly about `Query` vs `Exec`, this resource is crucial for understanding safe query parameterization, which is vital when using both methods.

Go Database/SQL Best Practices(documentation)

A collection of best practices for working with databases in Go, including tips on efficient query execution.

Mastering Go: Database Access(video)

A video tutorial that demonstrates database access in Go, likely covering `Query` and `Exec` in practical examples.