ProgramGuru

Executing SQL Queries

Start with Executing SQL Queries

In this lesson, explore executing sql queries through one small Go program, and connect its output to the language rules behind it.

What you’ll learn

  • Explain the main purpose of executing sql queries.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

db.QueryContext returns rows to iterate; db.QueryRowContext reads one row; db.ExecContext executes a statement without returning rows. Each call should carry a context and check errors.

The standard library passes query parameters separately from SQL text. Placeholder syntax differs by database driver (for example ? or $1).

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for sql queriesRows are a streaming resource requiring cleanup.SQL + argumentsStep 1QueryContextStep 2Scan each rowStep 3Close and check ErrStep 4Rows are a streaming resource requiring cleanup.
Rows are a streaming resource requiring cleanup.

Tools, syntax and practical notes

Real query pattern

rows, err := db.QueryContext(ctx, "SELECT id, title FROM tasks WHERE owner_id = ?", ownerID)
if err != nil { return err }
defer rows.Close()
for rows.Next() {
    var id int64
    var title string
    if err := rows.Scan(&id, &title); err != nil { return err }
    fmt.Println(id, title)
}
if err := rows.Err(); err != nil { return err }

Illustrative function-body snippet, requiring a configured db, ctx, and driver with ? placeholders. See Querying for data.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"database/sql"
	"fmt"
)

func main() {
	value := sql.NullString{String: "Ada", Valid: true}
	fmt.Println("Value:", value.String, "Present:", value.Valid)
}

Output

Value: Ada Present: true

Trace what the program does

sql.NullString models a nullable SQL column result. It is marked valid and prints Ada true; a real QueryContext and rows.Scan example is included separately.

Avoid common mistakes

  • Always check rows.Err after iteration and close rows.
  • Never concatenate untrusted text into the SQL query string.

Try it yourself

Write a function that queries tasks by owner using a parameter, scans each row, and checks rows.Err.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which method normally reads multiple SQL rows?
Choose one answer. You can try again.

Answer and explanation

QueryContext

Correct. sql.

What you should remember

You have practiced Executing SQL Queries in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: CRUD Operations →