Practical SQL Guide : The SELECT Dilemma (Part 1)
Recap...
It is Diwali 2024 today and I have nothing more to do rather than writing a blog down. Hoping that Diwali went good for you, let's re-begin the journey. And we will be going with the SELECT statement. There are lot of things about tables That I need to cover, but those can actually be explained when we have some idea of SELECT statement. Afterall you will use SELECT statement the most. So without further ado, let's begin.
The Vanilla SELECT
Vanilla is purest of flavors, nothing else. In software industry you will hear vanilla this vanilla that, often times, referring to the un-adulterated piece of software. In the similar manner, Vanilla SELECT refers to the very commonly unadulterated code of SELECT statement used in SQL. You have used it many times now, through this series. And that is, if you haven't guessed it yet, is :
SELECT * FROM <table_name>
It is the mostly used statement in SQL to be honest, and it doesn't contain any specification(flavor) to it. What it does? It simply SELECTS all the data(rows along with their column names) from the table and shows the output. Simple as Vanilla. Here's an example :
That's the most basic flavor of the SELECT statement. So what does SELECT statement do? It SELECTS! Now after can be used in two ways, for displaying data from a table, or to insert data into another table. We will talk about the first way in detail for sometime, and then we will later on speak about advanced SELECT statements.
What is '*' in the Vanilla SELECT?
If you have read something called RegEx or Regular Expression, then you would know that * represents zero or many. The concept of * is similar here, but little different, * means all the columns. So basically it says that 'SELECT ALL the data from ALL THE COLUMNS'. And yes until explicitly specified, remember that SELECT statement directly works on COLUMNS and not rows. So when you are using SELECT statement you are basically saying which column's data you want to choose rather than what rows you want to choose. Let's clarify this with the following example, and also learn about explicitly selecting a column.
So what happened here? I chose to SELECT(show) only the data from Name column. It's easy & logical to understand if you put little effort here. Let's do it for another column here. Ummm....Let's take Age?
Hope that get's you a little more idea about how the SELECT statement works. Now apart from single column, you can always explicitly specify which columns' data you want. For that all you need to do is to separate the column name with commas(','). Like I have done below:
I hope that you are already getting the hang of the SELECT statement. Okay I want to mention something here, that the order in which data is shown, is always the inverse order in which data has been inserted. That is the latest data will be first and the earliest data will be the last. This can be altered by specifying the order in which you want to SELECT the data. That's another topic!
That's it for now in this blog, there's a whole lot of topics to be covered in SELECT statement. Or may I say you are just about to learn the MOST powerful statements used along with SELECT statements altogether, beginning from here. We'll pace slowly otherwise it will be burst of knowledge :)
Comments
Post a Comment