Skip to content

Simple SELECT Queries in SQL: Your First Step :)

Hi everyone! How are you? I’m back at it, diving into small adventures in the coding world. Lately, I’ve been getting my hands dirty with SQL a bit. You know, sometimes you dive into a topic and initially it seems overwhelming, but then as you look deeper, you realize how simple and enjoyable it can be. That’s exactly how I felt about the SELECT part of SQL.

Thinking about it now, we all start somewhere, right? When I first encountered computers, even moving pixels on the screen seemed crazy. Now, we’re looking at the huge world of data behind those pixels. Of course, understanding what everything is at first takes some time. So in this article, I will try to explain SQL’s most fundamental building block, the SELECT command, as if we are chatting face-to-face. Shall we start?

If you ask me what SELECT does, in its simplest form, it allows you to fetch desired information from tables in a database. It’s like entering a huge library and searching only for books on a specific topic—SELECT does exactly that. It fetches only what we need from that massive pile of data. By the way, my favorite thing in a library is sometimes encountering a random book and getting lost in a new world. Similarly, in SQL, when writing queries, you can discover new information unknowingly, which is another kind of joy.

The most basic SELECT command looks like this:

SELECT * FROM TableName;

The asterisk (*) here indicates that we want all columns in that table. It’s like saying, get everything inside the table in front of us. For example, suppose you have a table called ‘Customers’. With this command, you list all your customers’ names, surnames, addresses, phone numbers—anything there. Isn’t that a lot? Sometimes, a single command giving access to so much information can feel strange. It’s like filling a big basket at the market.

What if we only want certain columns? For instance, only the customer’s name and surname? Then instead of the star, we write the names of the columns separated by commas:

SELECT CustomerName, CustomerSurname FROM Customers;

This way, we write cleaner, more focused queries. This also makes me think that in life, sometimes it’s better to request what we really need rather than everything, and knowing what we need makes things smarter. Isn’t that nice?

Now let’s move to a more interesting part. Sometimes, in databases, names are so strange that they can confuse you. For example, a table might be called ‘tbl_CustomerInfo_End’ or a column ‘Cust_Name’ abbreviated. In such cases, you must know both the correct table and column names when writing SELECT queries. What if you get it wrong? Nothing happens. Your query just gives an error. Small mistakes like this in my program can be frustrating but are useful in the end. Once I was working on a database schema, and I kept column names simple like ‘id’, ‘name’, ’email.’ Then a friend said, ‘Let’s make them more understandable, like ‘CustomerID’, ‘CustomerName’, ‘CustomerEmail’ again. He was right. I realized that sometimes simplicity is the best solution, but we shouldn’t compromise clarity. Anyway, back to our topic.

Besides, sometimes we don’t want to see all data in a table. For example, you only want to see entries meeting certain conditions, and that’s where the ‘WHERE’ clause comes in. WHERE acts like a filter. It brings only the data that matches your criteria. For example, if you want to list customers living in Ankara, you write:

SELECT * FROM Customers WHERE City = ‘Ankara’;

The ‘=’ sign means ‘equals.’ For text values, we use single quotes. For numeric values, we don’t. If we wanted to see customers over 30 years old, it would be:

SELECT * FROM Customers WHERE Age > 30;

With the WHERE clause, we can behave much more flexibly with data. It’s like a detective trying to find the right culprit with clues. Sometimes, you search for information in many places just to find one thing, and WHERE makes that search easier.

In conclusion, the SELECT command is like the entrance to SQL. Enter through this gate to reach your database’s information. It might seem foreign at first, but with practice, you’ll see how powerful and flexible it is. Learning these basic commands lays a solid foundation for writing more complex queries in the future. For example, I once made a simple reporting screen for a friend using just basic SELECT and WHERE commands to fetch the desired data and display it. Isn’t that great? In this tech-filled life, being able to read and interpret data correctly is very important.

If you’re curious, you can find more information here: Google-SELECT WHERE.

Remember, every great adventure begins with a small step. In SQL, this first step is the SELECT command. Go ahead, try it yourself and see how fun it can be!