DEV Community

Manoj Kumar
Manoj Kumar

Posted on

Solving HackerRank Basic Select — Query All Columns for a City by ID

If you are just starting out with SQL, HackerRank's Basic Select section is honestly the best place to begin. The problems are small, clean, and they train you to think in the right direction before things get complicated.

This one asked me to pull all the details of a city from a table called CITY where the ID is 1661. Nothing fancy, just a clean filter on a single condition.

My first instinct was SELECT star because the problem said query all columns. No point typing out every column name when star does the job in one character. Then I just needed to tell the database which table to look at and which row I actually want, so I added a WHERE clause with the ID.

Here is what I ended up writing:

SELECT *
FROM CITY
WHERE ID = 1661;
Enter fullscreen mode Exit fullscreen mode

That is really all there is to it. SELECT star gives you every column, FROM CITY points to the right table, and WHERE ID = 1661 makes sure you only get the one row you care about instead of the entire table dumping on you.

The easy problems feel too simple to take seriously but I think that is exactly why they matter. You are not just solving a problem, you are building the reflex of always asking yourself what rows do I actually need before you write anything. That habit quietly saves you a lot of trouble later on.

Top comments (0)