SQL For Beginners (Part2): From

Tram Ho

In this article we will learn about common types of FROM queries.

1. Setup

2. Tables

Usually the FROM clause will contain a list of tables and their join conditions. The simplest form that we can come across is when we get data from only one table.
As in the example below, we will retrieve all data of all columns in the EMPLOYEES table, which will be sorted by the value of the EMPLOYEE_ID column. Since we only fetch data from one table, the FROM clause will also contain only one table.

In many cases, the data we want to extract is in different tables. In the next example we will join the two tables EMPLOYEES and DEPARTMENTS together, provided that join is the DEPARTMENT_ID column of the two tables.

At Oracle, we can write a join in a different way. Instead of using the key-word JOIN and ON, the joined tables are separated by commas, and the join condition is specified in the WHERE clause. The query below gives the same result as the one above.

You can refer to the different join methods here

3. Inline Views

We can also encounter subquery in FROM clause, which are called inline view. After defining and assigning the alias, the inline view can be used as a normal table. The example below uses inline view to get data similar to the two examples above.

4. Clause WITH

There is another similar way to inline view, which is to pass the contents of the sub query to the WITH clause, and call it like a normal table. This helps to simplify the complicated and complicated queries, and also increases the legibility of the query.

5. Views

Another way to write a sub query is to create a view based on that query.

The view will hide part of the query complexity, making the rest look much simpler.

Refer: https://oracle-base.com/articles/misc/sql-for-beginners-the-from-clause

Share the news now

Source : Viblo