
For an instructor lead, in-depth look at learning SQL click below.
Structured Query Language (SQL) is a standard programming language for managing data held in a relational database management system (RDBMS). It’s used to perform operations on the database such as inserting, deleting, updating and retrieving data. In this blog post, we will focus on one fundamental part of SQL, the FROM clause. The FROM clause is used to specify the tables where SQL should retrieve or manipulate data.
Basic use of the FROM Clause
In its basic form, the FROM clause is simply used to specify the table from which data should be selected. Here’s a simple example:
|
1 2 3 |
SELECT name, age FROM Customers |
In this example, we’re selecting the ‘name’ and ‘age’ columns from the ‘Customers’ table.
Joining Tables with the FROM Clause
Apart from specifying the table to get data, the FROM clause is often used for joining multiple tables. Below is an example of how to join two tables ‘Orders’ and ‘Customers’ on the ‘customer_id’:
|
1 2 3 4 5 6 |
SELECT Customers.customer_name, Orders.order_date FROM Customers INNER JOIN Orders ON Customers.customer_id = Orders.customer_id |
In this statement, we’re using an INNER JOIN operation to combine rows from ‘Customers’ and ‘Orders’ tables where the ‘customer_id’ matches.
Alias Using FROM Clause
The FROM clause is also used to set aliases for our tables, which can be especially useful when using JOIN operations on multiple tables. An example:
|
1 2 3 4 5 6 |
SELECT c.customer_name, o.order_date FROM Customers AS c INNER JOIN Orders AS o ON c.customer_id = o.customer_id |
Here, we’ve set an alias for ‘Customers’ as ‘c’ and for ‘Orders’ as ‘o’. This helps us make our SQL statement more readable and tidy.
Conclusion
The power of SQL comes from its ability to interact with data directly where it resides, that is, in the database. The FROM clause is a vital part of SQL and mastering it can improve your efficiency in managing and working with your data. Keep practicing and soon you will master this essential SQL skill.
