Couple problems on SQL - 244 and 245

Back to General discussions forum

Rodion (admin)     2021-12-03 08:14:29
User avatar

Friends, Hi! Here are two first problems on SQL (and feel free to suggest more, since they are often encountered in interviews etc).

Introducing SQL

Aggregations in SQL

There easily could be flaws so feel free to tell about everything which looks suspicious to you.

Sorry for in-site sql runner is quite primitive, perhaps we'll decide to improve it in future.

P.S. I feel that though logically more complex, the second problem may be easier than the first, sorry for that.

Its Boft     2021-12-03 08:25:08
User avatar

Hello, Rodion.

I tried the first one using 2 select and it says "Error executing query: Error on preparing select". Are we supposed to use only 1 select?

Thanks.

gardengnome     2021-12-03 08:30:23
User avatar

Hi,

I also get the 'Error executing query: Error on preparing select' on a single select statement for the first problem even though the check via SQLite works without problems.

Rodion (admin)     2021-12-03 08:35:33
User avatar

Are we supposed to use only 1 select?

yep, honestly I'm not sure how are you going to combine two selects :) though you can use union probably if you want two.

thanks for hint, I'll add the note to problem statement.

on a single select statement

oops, sorry for this - I'll try to check what may be wrong with it and come back soon

UPD curious thing - it is substring function. it is not recognized by SQLite on server but seems to be ok for SQLite in the web-page. I guess, replace it with substr.

I shall try appending error text if any to error message.

Its Boft     2021-12-03 08:53:14
User avatar

Okay it works fine now. Thanks for the hint too, Rodion!

gardengnome     2021-12-03 08:56:00
User avatar

Great, that works. Yes, there are many flavours of SQL around...

Mister_Ed     2021-12-16 18:48:36

Hi,

just tried my first SQL problem (# 244) - WRONG ANSWER - while it gives the correct answer on my PC. The query is probably more complicated than it should be, but I'm just using one SELECT and UPPER, SUBSTR and INSTR. The answer says INSTR is causing the error, but that function is in the list of valid SQLite string functions.

Any ideas welcome!

Just another problem I should have noticed before, but I'm new to SQLite (just installed it this afternoon). The result of SQLite uses a vertical bar (C 'or') as a separator between columns, so I should probably replace all bars with an underscore. Correct?

Thanks for reading.

Rodion (admin)     2021-12-17 11:26:23
User avatar

Hi Friend!

It seems that sqlite version used on server for checking solutions is somewhat old:

select sqlite_version()     --  yields 3.36 in browser but 3.6.20 when submitted

I shall see what could be done about this, sorry. INSTR seemingly was added in 3.7.x - unlucky for us.

However unless you need a position of substring, you'd better use like '%bla%' instead!

but I'm new to SQLite

Me too :) but we haven't too much options for running sql safely on server side, so sorry for inconvenience!

The result of SQLite uses a vertical bar

Generally you shouldn't care about this. If you submit correct query, then the execution script should take care about parsing result etc (I don't remember exact details but you'll see if something is wrong in the answer when task is failed).

Also I dare to add that, unintentionally, first task on SQL is more complicated than second, so you may want try them in reverse order.

Mister_Ed     2021-12-17 15:24:29

My second trial gave the following result:

Got 'EDGAR,A,POE, JACK,,LONDON, NELLE,H,LEE, PETER,A,STOMP,'
instead of 'EDGAR_A_POE JACK_LONDON NELLE_H_LEE PETER_A_STOMP'

so maybe there IS a parsing issue? (I don't know why there are suddenly some italics.)

Rodion (admin)     2021-12-17 17:33:01
User avatar

according to problem statement you need to concatenate severa fields using underscores, seemingly you missed that part.

As for italics - this is caused by markdown format used for messages at forum... I fixed the message for you :)

Mister_Ed     2021-12-17 20:03:50

Ok, thanks so far!

The 2nd problem was definitely easier.

Anyway, I loaded the database p244.db and asked SELECT first_name from users;

William Martin Hillary Barack Abraham (ok with me)

then - select upper(first_name) from users;

WILLIAM MARTIN HILLARY BARACK ABRAHAM (still ok)

then - select upper(last_name) from users;

CLINTON KING CLINTON OBAMA LINCOLN (fine)

then - select upper(first_name) || '_' upper(last_name); (no backslashes but the C 'or' again for concatenating in SQLite)

Answer: in prepare, no such column: first_name (1)

So, how do you concatenate fields in SQLite?

Rodion (admin)     2021-12-17 20:49:10
User avatar

That's all right with || you just forget to use it between every parts being concatenated :)

select upper(first_name) || '_' || upper(last_name) ...

Not sure why you've seen error about column (or perhaps it was from different query?

as a side note you can use upper on the whole result rather than on every part.

Alexandr Milovantsev     2022-02-14 06:14:39

There is a little problem in the checker of problem #245: it rejects correct solution with following message: Got 'China,2,48 USA,2,48 Russia,2,36 UK,2,24 France,1,10' instead of 'USA,2,48 China,2,48 Russia,2,36 UK,2,24 France,1,10' :( As you can see? there is ambiguation in sort order of output.

Rodion (admin)     2022-02-14 09:38:53
User avatar

Alexandr, Hi! Glad to hear from you!

You are right, I missed that numbers could be equal... Added country name into sorting (and added this to problem statement). Hopefully it works... Thank you!

LukasDev     2022-07-21 12:22:00

Hey everybody, i wanted to know if someone has issues with Introducing SQL? I do have a functioning code that returns the correct result. But when i press submit i get: "more results than expected". On a side note i have a way cleaner code but that does not seem to work on version 3.36

sam_bandara     2022-11-04 00:09:04

Hi, problem with the #244. I only use following commands

        SELECT, IF, CONCAT and LEFT

I got the expected answer in SQL-fiddle but still checker says my answer is wrong.

Help please.

Cheers, Sam.

Rodion (admin)     2022-11-04 06:14:37
User avatar

Sam, Hi!

As discussed, SQL has many dialects which extend standard set of features. Regretfully they do it in different ways and are not compatible. Exercises use SQLite dialect. Your solution is not compatible with SQLite - it doesn't run in SQLfiddle (if you select any of two SQLite implementation there instead of default MySQL).

Moreover there is a button SQLite under solution box on this site itself, so you can try running your code before submitting. Error message "probably syntax error" is not much clarifying, of course, but it is easy to figure out what is wrong by removing parts of query step by step.

In your case it seems at least IF is not a part of SQLite dialect. Probably CONCAT too. Standard (sql-92) suggests CASE and || operator instead.

I'm sorry for this, but it is the lamentable situation in SQL world, often met in industrial practice - so I feel it is ok that exercises somewhat prepare us to it :)

(though I should have a look whether error message could be improved somehow)

sam_bandara     2022-11-10 00:55:44

Thanks Rodion!!! Job done

Esteban Arriaga     2024-04-03 00:22:16
User avatar

Hey Rodion, I have the same problema as MisterEd but it's not about italics, the thing is that those names that MisterEd mentions don't appear as part of the test-data, I write my query but that pulls only like "WilliamJClinton" like in the example. Could you tell me how can I solve this? Thank you for answering all our question before.

Rodion (admin)     2024-04-03 04:52:31
User avatar

Hi Esteban!

Thanks for contacting me, but please kindly note that generally "Summon Admin" button is for reporting issues, suggesting features etc - but not for "help me solve this task" :) Anyway I don't remember all the problems well myself :)

Now to your question. Honestly, you'd better try to explain yourself a bit more careful in future :) E.g. mention which exactly task you are speaking about, what you did and which issue you have, like this:

You are tryin to solve "Introducing SQL" (right?) and have written some code in SQL, which works almost well (good!)

The issue is that your code returns people who have exactly three names (first, middle, last), but does not return people who have only two names (without middle name). So for table with two persons:

insert into users values ('William', 'Jefferson', 'Clinton', null);
insert into users values ('Abraham', null, 'Lincoln', null);

your code returns only WILLIAM_J_CLINTON but misses ABRAHAM_LINCOLN. According to problem statement and example this is not correct.

Check your code and think how it works, try to identify which condition filters out the second name. Try removing your CASE... temporarily and select just first_name AS plaque_name.

I think I see how to fix it but I'm reluctant to tell you. You'll gain much more experience if you find it yourself (I believe you can! it seems easy - just spend bit more time on debugging).

You know, to learn programming means not only to write code (even school kids write code nowadays) - but to be able to find mistakes in the code and fix them - i.e. to debug - that is what makes you professional, that is why employers will pay you big wages in future :)

Please login and solve 5 problems to be able to post at forum