Up: Query Building
Next: INSERT >

Query Building: SELECT

Contents

Basic example

  contents
SQL
SELECT u.user_id, u.name, u.email
FROM public.users AS u
WHERE u.name = 'bob';
Go
u := tables.USERS().As("u")
var user User
var users []User

// ...if you use ToSQL
query, args := sq.Select(u.USER_ID, u.NAME, u.EMAIL).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    ToSQL()
rows, err := DB.Query(query, args...)
if err != nil {
    /* handle error */
}
defer rows.Close()
for rows.Next() {
    err = rows.Scan(&user.UserID, &user.Name, &user.Email)
    if err != nil {
        /* handle error */
    }
    users = append(users, user)
}

// ...if you use Selectx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
err := sq.Selectx(func(row *sq.Row) {
        user.UserID = row.Int(u.USER_ID)
        user.Name = row.String(u.NAME)
        user.Email = row.String(u.EMAIL)
    }, func() {
        users = append(users, user)
    }).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    Fetch(DB)
if err != nil {
    /* handle error */
}

// ...if you use SelectRowx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
// SelectRowx only selects one row
err := sq.SelectRowx(func(row *sq.Row) {
        user.UserID = row.Int(u.USER_ID)
        user.Name = row.String(u.NAME)
        user.Email = row.String(u.EMAIL)
    }).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    Fetch(DB)
if err != nil {
    /* handle error */
}

SELECT DISTINCT

  contents
SQL
SELECT DISTINCT u.user_id, u.name
FROM public.users AS u
WHERE u.name = 'bob';
Go
u := tables.USERS().As("u")
var user User
var users []User

// ...if you use ToSQL
query, args := sq.SelectDistinct(u.USER_ID, u.NAME).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    ToSQL()
rows, err := DB.Query(query, args...)
if err != nil {
    /* handle error */
}
defer rows.Close()
for rows.Next() {
    err = rows.Scan(&user.UserID, &user.Name)
    if err != nil {
        /* handle error */
    }
    users = append(users, user)
}

// ...if you use Selectx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
err := sq.SelectDistinct().
    Selectx(func(row *sq.Row) {
        user.UserID = row.Int(u.USER_ID)
        user.Name = row.String(u.NAME)
    }, func() {
        users = append(users, user)
    }).
    From(u).
    Where(u.USER_ID.EqString("string")).
    Fetch(DB)
if err != nil {
    /* handle error */
}

// ...if you use SelectRowx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
// SelectRowx only selects one row
err := sq.SelectDistinct().
    SelectRowx(func(row *sq.Row) {
        user.UserID = row.Int(u.USER_ID)
        user.Name = row.String(u.NAME)
    }).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    Fetch(DB)
if err != nil {
    /* handle error */
}

SELECT DISTINCT ON

  contents

Note: SELECT DISTINCT ON is a postgres-only feature.

SQL
SELECT DISTINCT ON (u.email, u.user_id) u.user_id, u.name
FROM public.users AS u
WHERE u.name = 'bob';
Go
u := tables.USERS().As("u")
var user User
var users []User

// ...if you use ToSQL
query, args := sq.SelectDistinctOn(u.EMAIL, u.USER_ID)(u.USER_ID, u.NAME).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    ToSQL()
rows, err := DB.Query(query, args...)
if err != nil {
    /* handle error */
}
defer rows.Close()
for rows.Next() {
    err = rows.Scan(&user.UserID, &user.Name)
    if err != nil {
        /* handle error */
    }
    users = append(users, user)
}

// ...if you use Selectx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
err := sq.SelectDistinctOn(u.EMAIL, u.USER_ID)().
    Selectx(func(row *sq.Row) {
        user.UserID = row.Int(u.USER_ID)
        user.Name = row.String(u.NAME)
    }, func() {
        users = append(users, user)
    }).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    Fetch(DB)
if err != nil {
    /* handle error */
}

// ...if you use SelectRowx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
// SelectRowx only selects one row
err := sq.SelectDistinctOn(u.EMAIL, u.USER_ID)().
    SelectRowx(func(row *sq.Row) {
        user.UserID = row.Int(u.USER_ID)
        user.Name = row.String(u.NAME)
    }).
    From(u).
    Where(u.USER_ID.EqString("bob")).
    Fetch(DB)
if err != nil {
    /* handle error */
}

SELECT EXISTS

  contents
SQL
SELECT EXISTS (
    SELECT 1
    FROM public.users AS u
    WHERE u.name = 'bob'
);
Go
u := tables.USERS().As("u")
var exists bool

// ...if you use ToSQL
query, args := sq.Select(sq.Exists(
    sq.SelectOne().From(u).Where(u.USER_ID.EqString("bob")),
)).ToSQL()
err := DB.Query(query, args...).Scan(&exists)
if err != nil {
    /* handle error */
}

// ...if you use SelectRowx (see [Struct Mapping](/sq/basics/struct-mapping.html#select-one-vs-many))
err := sq.SelectRowx(func(row *sq.Row) {
    exists = row.Bool(sq.Exists(
        sq.SelectOne().From(u).Where(u.USER_ID.EqString("bob")),
    ))
}).Fetch(DB)
if err != nil {
    /* handle error */
}

Clause manipulation

  contents
Clauses are just struct fields, you can either use method chaining to add clauses or manipulate the struct fields directly.
Clauses: WITH, SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, WINDOW, ORDER BY, LIMIT, OFFSET
Up: Query Building
Next: INSERT >