Does the order we select the columns affect anything?

Question

In the context of this exercise, does the order we select the columns affect anything?

Answer

No, the order that columns are selected does not affect anything other than what order the columns are shown in the result set.

So, whether you selected the columns as
SELECT col1, col2
or
SELECT col2, col1,

if the rest of the SQL statement is the same, then the only difference is the column order.

Usually, columns are selected based on a logical and meaningful order. For instance, when selecting user information, the first column is usually some identifier, like user_id, or name, followed by the rest of the information.

4 Likes

Just trying to clarify my understanding of FROM and the relationship with JOIN clauses.

SELECT chooses the columns from the table(s?) identified by FROM… But in the context of this exercise, we only list one table in the FROM clause… does that mean that JOIN, LEFT JOIN, CROSS JOIN, & UNION effectively extend the FROM clause when we are working with multiple tables?

I’m just trying to visualize what’s going one. Thanks.

1 Like

That’s what I’ve also observed and seems to be pretty much what happens every time it’s used: SELECT queries the combined or resulting tables after a FROM and JOIN clause. To my understanding, it doest not necessarily mean the SELECT function is being extended but that the two tables result in one new table on which the clause applies the query.

Good evening. As I am improving my understanding a question arises regarding joining tables;

what is the difference from selecting data from multiple tables (i.e. select table.column, table2.column) and using that as my query rather than using joins? I understand joins, but not really clear when or how?

Please elaborate or help. Please and thank you.

/v/r
Lazlo P.

SELECT premium_users.user_id,
plans.description
FROM premium_users
JOIN plans
ON plans.id = premium_users.membership_plan_id;

I don’t understand JOIN from the question asked in the problem. Am I selecting all the things from the table to put into the query, and then JOINing them to the next part. If possible, a thorough explanation would be highly appreciated

Essentially yes in this case. You are identifying the columns you want selected and then specifying the tables the data is located in. The JOIN connects the plans table which contains the plan description.

SELECT premium_users.user_id             -- User
,             plans.description          -- Plan description
FROM  premium_users                      -- table of premium_users
JOIN     plans                           -- table of plans
ON       plans.id = 
         premium_users.membership_plan_id;  -- use to connect the tables together

I hope that helps.

1 Like

Hello, hope someone can help me understand better, what’s the different from this solution below :-

select plans.description,

premium_users.user_id

from plans

join premium_users

on plans.id = premium_users.membership_plan_id;

Hello @dev1727200404,

If you are asking if the order of tables impacts the query, in this particular case, it does not.

Depending on the RDBMS, the order of the tables could impact the performance.

3 Likes

Yes, that’s correct! The FROM clause specifies the table(s) from which you want to retrieve data. If you only list one table in the FROM clause, then that table is the only source of data for your query.

JOIN, LEFT JOIN, CROSS JOIN, and UNION are used to combine data from multiple tables, effectively extending the FROM clause to include multiple tables.

For example, if you want to retrieve data from two tables, users and orders, you could write a JOIN clause like this:

SELECT *
FROM users
JOIN orders ON users.id = orders.user_id

This JOIN clause combines rows from the users and orders tables where the id column in the users table is equal to the user_id column in the orders table.

I hope that helps clarify things! Let me know if you have any more questions.

1 Like