sql.ErrNoRows
GoINFOCommonDatabase

sql: no rows in result set

Quick Answer

Check errors.Is(err, sql.ErrNoRows) after QueryRow().Scan() to handle the not-found case explicitly.

What this means

Returned by Row.Scan when a query returns zero rows. It is not a database error; it means the query succeeded but matched nothing.

Why it happens
  1. 1QueryRow matched zero rows in the table
  2. 2WHERE clause filters out all existing records

Fix

Map to domain not-found error

Map to domain not-found error
err := db.QueryRow(q, id).Scan(&user)
if errors.Is(err, sql.ErrNoRows) {
    return nil, ErrNotFound
}

Why this works

Mapping sql.ErrNoRows to a domain-level ErrNotFound keeps the database layer encapsulated.

Code examples
Detectgo
var name string
err := db.QueryRow(
    "SELECT name FROM users WHERE id=?", 1,
).Scan(&name)
if errors.Is(err, sql.ErrNoRows) {
    fmt.Println("not found")
}
Map to domain errorgo
if errors.Is(err, sql.ErrNoRows) {
    return nil, ErrUserNotFound
}
Query vs QueryRowgo
rows, err := db.Query("SELECT id FROM t")
// Query never returns ErrNoRows
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors