Understanding SQL's SELECT and FROM Commands.

Understanding SQL's SELECT and FROM Commands.

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.

No alt text provided for this image
table in database : Customers

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.

No alt text provided for this image
SQL query to select a single column (record) from a table.

Output for the above query:

No alt text provided for this image
Result of the above mentioned 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.

No alt text provided for this image
SQL query to selec a couple of columns (records/features) from a given table.

Output for the above query:

No alt text provided for this image
Result of the above query - 2 columns retreived.

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.

No alt text provided for this image
Multiple columns selection in a SQL query.

Result of the above query:

No alt text provided for this image
Result of the above query - 4 columns retreived as mentioned in 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 –

No alt text provided for this image
SQL query to select everything from a table.

Output:

The whole table comes in our output this time.

No alt text provided for this image
All of the table (the table itself in other words) retreived as a result of above mentioned query.

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!






要查看或添加评论,请登录

社区洞察

其他会员也浏览了