Inline view in SQL is like a select statement that contains everything in the From clause.
View is temporary table that is created for data manipulation.
In this we don't specify the table name instead of that the query or source from which data will come will be specified.
The general syntax for an Inline view is
SELECT "column_name" FROM (Inline View)
Need for Inline View
Assume we have two tables: The first table is User_Address, which maps each user to a ZIP code; the second table is User_Score, which records all the scores of each user.
The question is, how to write a SQL query to find the number of users who scored higher than 200 for each ZIP code
Without using Inline View
CREATE TABLE User_Higher_Than_200
SELECT User_ID, SUM(Score) FROM User_Score
GROUP BY User_ID
HAVING SUM(Score) > 200;
SELECT a2.ZIP_CODE, COUNT(a1.User_ID)
FROM User_Higher_Than_200 a1, User_Address a2
WHERE a1.User_ID = a2.ZIP_CODE
GROUP BY a2.ZIP_CODE;
Using inline view
SELECT a2.ZIP_CODE, COUNT(a1.User_ID)
FROM
(SELECT User_ID, SUM(Score) FROM User_Score GROUP BY User_ID HAVING SUM(Score) > 200) a1,
User_Address a2
WHERE a1.User_ID = a2.ZIP_CODE
GROUP BY a2.ZIP_CODE
0 Comment(s)