# Is there an idiomatic way to select a single value from a SQL query?

**URL:** https://discourse.julialang.org/t/is-there-an-idiomatic-way-to-select-a-single-value-from-a-sql-query/68962
**Category:** Data
**Tags:** sqlite, sql
**Created:** [September 29, 2021, 8:57pm UTC](https://discourse.julialang.org/t/is-there-an-idiomatic-way-to-select-a-single-value-from-a-sql-query/68962 "2021-09-29T20:57:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [September 29, 2021, 8:57pm UTC](https://discourse.julialang.org/t/is-there-an-idiomatic-way-to-select-a-single-value-from-a-sql-query/68962/1 "2021-09-29T20:57:06Z")

</div>

Hi,

Some SQL queries (like “SELECT COUNT(\*)…”) return a single value. The shortest way I’ve managed to come up with for obtaining the result of the query is:

```julia
using SQLite
db = SQLite.DB()
SQLite.DBInterface.execute(db, "CREATE TABLE t (field TEXT)")
count = iterate(SQLite.DBInterface.execute(db, "SELECT COUNT(*) FROM t"))[1][1]

```

That last line is a bit of a mouthful. Is there are better way to do this?

Thanks!  
Geoff

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [September 29, 2021, 9:33pm UTC](https://discourse.julialang.org/t/is-there-an-idiomatic-way-to-select-a-single-value-from-a-sql-query/68962/2 "2021-09-29T21:33:04Z")

</div>

`DBInterface` is exported from SQLite.jl, so you could do:

```julia
counts = first(first(DBInterface.execute(db, "SELECT COUNT(*) FROM t")))

```

you could also define this as a local function in our app/script if you’ll re-use it a lot:

```julia
count(db, tbl) = first(first(DBInterface.execute(db, "SELECT COUNT(*) FROM $tbl")))

```

---

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [September 29, 2021, 10:54pm UTC](https://discourse.julialang.org/t/is-there-an-idiomatic-way-to-select-a-single-value-from-a-sql-query/68962/3 "2021-09-29T22:54:54Z")

</div>

Thanks!

I wasn’t too worried about the `SQLite.DBInterface...` bit, it was the `iterate()[1][1]`, and `first(first())` is nicer. Good to know there’s not a `DBInterface.execute_but_get_me_one_result` and I’ll wrap `first(first()) ` in something a bit prettier.

Cheers,  
Geoff

---

<div class="post-metadata">

### Author: ![Hecht](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Hecht](https://discourse.julialang.org/u/Hecht)
#### Post date: [October 1, 2021, 7:59am UTC](https://discourse.julialang.org/t/is-there-an-idiomatic-way-to-select-a-single-value-from-a-sql-query/68962/4 "2021-10-01T07:59:23Z")

</div>

_The value_ -add provided by _the_ Spring Framework’s JDBC abstraction This class executes _SQL queries_ , update _statements_ or stored procedure calls.

[One Vanilla](https://www.onevanilla.run/)
