Skip to content

Instantly share code, notes, and snippets.

@simonw

simonw/sql.md Secret

Created May 1, 2025 18:41
Show Gist options
  • Save simonw/db129dddb76e5ba8f97794a794ae626d to your computer and use it in GitHub Desktop.
Save simonw/db129dddb76e5ba8f97794a794ae626d to your computer and use it in GitHub Desktop.

2025-05-01T18:39:07 conversation: 01jt6jakja3r8ydeamv756t12w id: 01jt6jakjcsmhwa2qsqs0abcp8

Model: mlx-community/Qwen3-1.7B-6bit

Prompt

SQL schema:

create table repos (id int primary key, name text);

SQLite query to count repos with names starting with "datasette-"

Response

<think>
Okay, let's see. The user wants an SQLite query to count the number of repos whose names start with "datasette-".

First, I need to recall how to use the LIKE operator in SQLite. The LIKE operator is used to search for a pattern in a column. The syntax is something like SELECT * FROM table WHERE column LIKE 'pattern'.

So, the table is called 'repos' and the column is 'name'. The pattern here is "datasette-". But wait, in SQLite, the LIKE operator is case-insensitive by default? Or does it depend on the collation? Hmm, the user didn't specify, but maybe they want it case-sensitive. But the problem says "starting with", so maybe they want it to match exactly. However, the user didn't mention case sensitivity. But since the example is in the problem, perhaps the intended answer is case-sensitive. But maybe the user wants it to match any case. But I think in most cases, unless specified, it's case-sensitive. So I'll proceed with that.

So the query would be SELECT COUNT(*) FROM repos WHERE name LIKE 'datasette-';

But wait, maybe the user wants to make sure that the pattern is exact. For example, if the name is 'datasette-foo', then it would match. But if the name is 'datasette', then it wouldn't. So the pattern is correct.

Another thing to consider: the primary key is 'id', but the name is the column. So the query is correct.

So the final query would be SELECT COUNT(*) FROM repos WHERE name LIKE 'datasette-';
</think>

To count the number of repositories (reps) whose names start with the string "datasette-", you can use the LIKE operator in SQLite. The query will filter rows where the name column matches the pattern datasette- and then count the number of such rows.

Here is the SQL query:

SELECT COUNT(*) 
FROM repos 
WHERE name LIKE 'datasette-';

Explanation:

  • COUNT(*) returns the total number of rows that match the condition.
  • name LIKE 'datasette-' matches any string that begins with "datasette-".
  • The WHERE clause filters the rows to only include those with names starting with "datasette-".

This query assumes that the name column contains strings and that the comparison is case-sensitive (unless specified otherwise, which is the default in SQLite). If you want to make the match case-insensitive, you could use LOWER(name) LIKE LOWER('datasette-'), but that is not required unless specified.

2025-05-01T18:41:01 conversation: 01jt6jebkx5ktv2vc3hh2awqcf id: 01jt6jebm0zyqemk5cdkz2397q

Model: mlx-community/Qwen3-8B-4bit

Prompt

SQL schema:

create table repos (id int primary key, name text);

SQLite query to count repos with names starting with "datasette-"

Response

<think>
Okay, let me try to figure out how to write this SQL query. The user wants to count the number of repos whose names start with "datasette-". The schema given is a table called repos with an id and a name column.

First, I remember that in SQL, to count rows that match a certain condition, you use the COUNT() function along with a WHERE clause. So the basic structure would be SELECT COUNT(*) FROM repos WHERE name LIKE 'datasette-%'.

Wait, the LIKE operator is used here. Since the name needs to start with "datasette-", the pattern would be 'datasette-%'. The percent sign (%) is a wildcard that matches any sequence of characters. So that should work.

But I should make sure that the string is case-sensitive. In SQLite, the default for the LIKE operator is case-insensitive unless you use the COLLATE NOCASE. But the problem says the names start with "datasette-", so if the names are stored exactly as such, then the query should work. However, if there are variations in case, like "Datasette-", the query might not catch them. But the question doesn't mention case sensitivity, so I'll assume the names are stored exactly as "datasette-".

Another thing to consider: are there any leading or trailing spaces in the name? The problem states the names start with "datasette-", so probably not. But the LIKE operator would still work as intended.

So putting it all together, the query would be:

SELECT COUNT(*) FROM repos WHERE name LIKE 'datasette-%';

That should count all the repos where the name starts with "datasette-". Let me double-check if there's any other possible issues. For example, if the name is exactly "datasette-", it would be included. If the name is longer, like "datasette-example", it would also be included. That's correct.

I think that's the right approach. Let me make sure there's no typo in the string. The user wrote "datasette-", so the query uses that exact string. Alright, that should do it.
</think>

To count the number of repositories (repos) whose names start with "datasette-", you can use the LIKE operator in SQLite with a wildcard (%) to match any characters following the prefix. Here's the correct query:

SELECT COUNT(*) 
FROM repos 
WHERE name LIKE 'datasette-%';

Explanation:

  • SELECT COUNT(*) – Counts the number of matching rows.
  • FROM repos – Specifies the table to query.
  • WHERE name LIKE 'datasette-%' – Filters rows where the name field starts with "datasette-". The % wildcard matches any sequence of characters (including none), ensuring that names like "datasette-example" or "datasette-123" are included.

Notes:

  • This query is case-insensitive by default in SQLite unless you use COLLATE NOCASE explicitly.
  • If you want to ensure exact prefix matching, this query is appropriate. If you need a case-sensitive match, you would need to adjust the query accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment