Understanding SQL's SELECT and FROM Commands.
Saud Ahmad
Analytics - Consultant | PG Diploma (Applied Statistics) | Diploma ( Data Science) | MA(Economics with Econometrics) | Python | SQL | SAS | Power Bi | Tableau |
Introduction to SQL SELECT and FROM Commands:
As we now understand what SQL is, let’s now begin with writing simple and small SQL queries and understand their meanings. We will first and foremost discuss SQL SELECT and FROM commands in this article.
SELECT:
Select command is used to retrieve data from a database table.
FROM:
From command is used to specify the table name, from which data (columns mentioned in SELECT command) will be retrieved.
Let’s say we have a table named “Customers” table in our relational database which has customer’s car purchasing details.
There is an analyst who wants to retrieve data of customer names only from this table, as you can see in the table we have total 6 columns, however the analyst only wants to retrieve only customer names from this table and nothing else is required.
Let’s understand how he can achieve this result. This is where the SQL SELECT command comes into picture. The analyst writes below mentioned query to get his desired output from the given table.
Output for the above query:
Here:
SELECT:
This command will select (retrieve) only one desired column from our table.
FROM:?
The above-mentioned column will be retrieved from customers table. We’ve mentioned our source data set name here, which is customers table in this case.
; - Semicolon
This is the query terminator that we use in PostgreSQL.
Now let’s say the analyst wants to explore more features(columns/records) from the given table customers, this time he wants to see what type of a car (car model name) a customer has bought along with a customer name. He writes below mentioned query this time to get this desired result.
Output for the above query:
This time he has added another column model name from the table (defined to tell a car’s model name in the table) in his previous query.
We use (,) - comma in our SQL queries to separate our column names.
Similarly, we can select any number of columns as per our need from a given table. ?Like we have selected 4 columns here, customer name, ordered year for a car, car model name and car’s production company name.
Result of the above query:
Now let’s say the analyst wants to select everything (all columns) from the table – we use asterisk (*) to select all features from the table. His final query for today to retrieve all of the information from the table would be like this –
Output:
The whole table comes in our output this time.
This time we have not specified any column name, instead we have mentioned asterisk (*) only with SELECT command – we can the that the output is our complete table.
So that’s it for today. See you again.
Thank you for reading!