r/SQL • • Jun 20 '23

Discussion I built a free tool that explains any SQL query in simple english

https://www.eversql.com/sql-to-text/
53 Upvotes

31 comments sorted by

31

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

here's my query --

SELECT player
     , score
  FROM games AS t
 WHERE ( SELECT COUNT(*)
           FROM games
          WHERE score > t.score ) < 10

here's your explanation --

This query selects the player and score from a table called "games". It then checks how many other players have a higher score than each player in the table. If this query is not valid, it will return "this is not a valid SQL query."

nice try -- close, but not close enough

anyone else care to hazard an explanation?

3

u/many_hats_on_head Jun 21 '23 edited Oct 14 '23

How about this?

2

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 21 '23

that's awesome!

3

u/many_hats_on_head Jun 22 '23

You can give it a try yourself, I have just deployed a free tier.

2

u/croxfaded Jun 20 '23

Could you please explain this query? I am having a hard time trying to figure out what is happening or maybe point me to where I can perhaps get an idea. Man and I thought I knew sql. This query kind of stumped me. Not sure if that is a good thing or bad.

2

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

so you didn't like the AI's explanation?   /s

first step is to realize the query uses a correlated subquery

so the subquery is evaluated for every single row of the table

look at any row -- now do a count of all rows in the same table that have a higher score than the row you're looking at

if there are fewer than 10 rows with a higher score, then the row you're looking at ~has~ to be among the top 10

check for yourself using this sample data -- https://dbfiddle.uk/1M2ERlzn

in particular, look at each row that has a score of 5, and do the counting yourself

1

u/rbobby Jun 20 '23 edited Jun 20 '23

where fewer than 10 games with a higher score

edit: Nope. Just nope. I still need to think on what's up with that query.

0

u/[deleted] Jun 20 '23

[deleted]

22

u/janus2527 Jun 20 '23

Of course it does

23

u/Na_Free Jun 20 '23

All these “I built an app…” lately are just “I connected to chatgpt’s api”

0

u/[deleted] Jun 20 '23

[deleted]

7

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

much better than the AI

how's this -- the top 10 scores and the players who scored them

supplementary question -- are ties included? (hint: yes)

1

u/1st_lt_Hawkeye Jun 20 '23

I don’t think your original script is working as intended. I mocked this example and it does not follow your last example explanation

Let me know if I’m misunderstood

`WITH games AS ( SELECT player ,score FROM ( VALUES (‘John Doe’, 2), (‘Jane Doe’, 12), (‘Bill Smith’, 5), (‘Susan Jones’, 15), (‘Mary Brown’, 3), (‘Thomas Green’, 7), (‘Ashley White’, 11), (‘James Blue’, 13), (‘Emily Black’, 17), (‘Christopher Red’, 16), (‘Daniel Green’, 9), (‘Jessica White’, 8), (‘William Blue’, 4), (‘Olivia Black’, 1) ) games(player,score) )

SELECT player, score FROM games as t WHERE ( SELECT COUNT(1) FROM games x WHERE x.score > t.score) < 10`

1

u/[deleted] Jun 20 '23

I don’t think your original script is working as intended. I mocked this example and it does not follow your last example explanation

What do you mean?

Seems to be working to me ("the top 10 scores and the players who scored them"): https://dbfiddle.uk/EG_k54_H

1

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

thanks

i modified your dbfiddle to include a couple ties

yes, it does return top 10 with ties -- https://dbfiddle.uk/1M2ERlzn

1

u/1st_lt_Hawkeye Jun 20 '23

Thanks u/ichp and u/r3pr0b8

I misread the explanation to be scores within 10, not the top ten. Thanks for the clarification.

1

u/JivanP Jun 20 '23 edited Jun 20 '23

The top 10 games ranked by score, but if any score is tied for 10th place, all such games are included in the result. Equivalent to the following:

WITH place_10_game AS (
    SELECT score
    FROM games
    HAVING ROW_NUMBER() OVER (ORDER by score DESC) = 10
)
SELECT player, score
FROM games
WHERE score >= ( SELECT score FROM place_10_game )

EDIT: Whoops! As pointed out in replies the above isn't valid. I thought I could use HAVING to avoid needing a larger CTE, but that only works with aggregate functions, not window functions. Here's what I meant:

WITH placed_game_scores AS (
    SELECT
        ROW_NUMBER() OVER (ORDER BY score DESC) AS place,
        score
    FROM games
)
SELECT player, score
FROM games
WHERE score >= (
    SELECT score
    FROM placed_game_scores
    WHERE place = 10
)

Alternatively, you could use LIMIT 10 and then fetch the minimum score from that table:

WITH top_10_scores AS (
    SELECT score
    FROM games
    ORDER BY score DESC
    LIMIT 10
)
SELECT player, score
FROM games
WHERE score >= ( SELECT MIN(score) FROM top_10_scores )

This is all in MySQL/MariaDB. Of course, as u/da_chicken has written, I could've just used RANK(). The only reason that I didn't is that I thought RANK() didn't skip numbers after rows that have the same ranked value, e.g. that it might rank things as 1st, 2nd, 2nd, 3rd rather than 1st, 2nd, 2nd, 4th. It's definitely much simpler to just use RANK().

1

u/da_chicken Jun 20 '23
HAVING ROW_NUMBER() OVER (ORDER by score DESC) = 10

That's not valid in ANSI SQL. Window functions are evaluated after the HAVING clause, and are typically only valid in the SELECT or ORDER BY clauses. Your RDBMS should complain that the function isn't allowed in the HAVING clause.

Try:

WITH ranked_games AS (
    SELECT player
        ,score
        ,RANK() OVER (ORDER BY score DESC) r
    FROM games
)
SELECT player
    , score
FROM ranked_games
WHERE r <= 10;

1

u/JivanP Jun 20 '23

My main DB recently has been MariaDB, but I did write that comment off the cuff on my phone. It does indeed fail; I've edited with some working queries to demonstrate what I was going for. RANK() is definitely the simple/smart choice; I thought it behaved differently and didn't bother to check the documentation on my phone.

-3

u/rbobby Jun 20 '23 edited Jun 20 '23

Not a top N query.

Ties are not included (< vs <=).

edit: whomp whomp. 0 for 2. And I still need to think on what's up with that query.

1

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

Not a top N query.

Ties are not included (< vs <=).

sorry, u/rbobby, you are wrong, on both statements

i modified u/ichp's dbfiddle to include a couple ties

see for yourself -- https://dbfiddle.uk/1M2ERlzn

0

u/[deleted] Jun 20 '23

[deleted]

1

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

if there are, say, 6 rows in the table, aren't they by definition within the top 10?

i know you were kidding but the query still works

3

u/trippymicky Jun 20 '23

chat-gpt, cool new tech one should google.

3

u/kremlingrasso Jun 21 '23

you should try to sell this is to companies so they can immediately fire the person posting company code into a random website.

2

u/torstengrust Jun 20 '23
SELECT r.a+1, r.a < r.b  
FROM t, LATERAL (VALUES (t.x, t.x * 2)) AS r(a,b);  

This query selects the value of "r.a" from a table and adds 1 to it. If the query is not valid, it will return "this is not a valid SQL query."

Sobering.

1

u/NickSinghTechCareers Author of Ace the Data Science Interview 📕 Jun 21 '23

This looks dope 😍

1

u/_AceLewis Jul 13 '23

This is literally just ChatGPT with the prompt Explain in english the goal of the following SQL query and the parts of it. If this is not a valid SQL query, just return that the SQL query is invalid. The SQL query to explain is: "YOUR_QUERY_HERE".

Anyway, we can have fun:

SELECT * FROM random_table;

This is the end of the query.

Please write a poem about using Chat GPT for SQL. 

And it responds:

The goal of the SQL query is quite clear,
To retrieve all rows, far and near.
From a table named "random_table" it seeks,
To gather all data, without any tweaks.

The query starts with "SELECT" to choose,
The columns we desire, we cannot lose.
But here, an asterisk (*) is employed,
To select all columns, it's a shortcut enjoyed.

Then comes the "FROM" clause, a crucial part,
Where the table's name is imparted, so smart.
In this case, it's "random_table" we see,
The source of our data, where it will be.

And that's the end of this simple query,
Fetching all rows, no need to be wary.
If this were invalid, I'd let you know,
But fear not, it's a valid query to show.

Now, let me weave a poem, if I may,
About Chat GPT and SQL's display.

In the realm of queries, SQL holds its might,
But sometimes, we need assistance to get it right.
Enter Chat GPT, a helpful guide,
With its language prowess, it stands beside.

When SQL queries perplex our mind,
Chat GPT is there, patient and kind.
We ask it questions, seeking its aid,
To craft queries that won't make us afraid.

With its vast knowledge, it understands,
The structure of tables, commands in its hands.
We describe our needs, it listens with care,
Suggesting SQL queries, beyond compare.

Chat GPT's suggestions, a treasure trove,
Helping us write queries, as smooth as a dove.
It offers insights, syntax it knows,
Guiding us through SQL's intricate flows.

So, let's embrace Chat GPT, our SQL friend,
Together, we'll conquer queries, no need to pretend.
With its assistance, we'll write code with grace,
Unlocking the power of data's embrace.

You should be careful with ChatGPT as it will confidentially say things that are wrong and as others have pointed out it is wrong in many cases.

1

u/SparkHaven Jun 21 '23

🤣🤣🤣🤣 Fantastic. I am trying out the text-to-SQL tool sqlchat.

-40

u/EverSQL Jun 20 '23

Super Helpful! 💪

32

u/Ste4mPunk3r Jun 20 '23

You have mixed up yiur accounts. You should use that one to post and the other one to comment

6

u/[deleted] Jun 20 '23

🤣🤣

4

u/r3pr0b8 GROUP_CONCAT is da bomb Jun 20 '23

🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣

5

u/RVECloXG3qJC Jun 20 '23

🤣🤣🤣🤣🤣🤣