Preview

Subquery or Join - Which One Is Better to Use

Satisfactory Essays
Open Document
Open Document
374 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Subquery or Join - Which One Is Better to Use
There are ongoing arguments among experts about the superiority of sub-query vs join. Both sides have their reasons to support their position. From my point of view, there are several axis of being "better"--

→better performance
→better readability
→better maintainability
→better flexibility and so on

In JOINs RDBMS can create an execution plan that is better for our query and can predict what data should be loaded to be processed and save time, unlike the sub-query where it will run all the queries and load all their data to do the processing.

The good thing in sub-queries is that they are more readable than JOINs: that's why most new SQL people prefer them; it is the easy way; but when it comes to performance, JOINS are better in most cases even though they are not hard to read too. In most cases JOINs are faster than sub-queries and it is very rare for a sub-query to be faster.

Now-a-days, most databases includes it as an optimization step to convert sub-queries into joins when it is analyzing our query; this indeed gives a better performance. Let’s use the following example to predict which gives the better performance:

Let’s say, we have 3 tables related to each other, and we need to select data from one table that has some fields related to the other 2 tables. To perform the task, we designed 2 SQL statements; one with one with JOIN and another with standard sub-query. We now have to decide which will perform better to do this specific task:

Query-1 (with JOIN):

SELECT Table1.City, Table1.State, Table2.Name, Table1.Code, Table3.ClassName
FROM Table1 INNER JOIN Table2 ON Table1.EmpId = Table2.Id INNER JOIN Table3 ON Table1.ClassId = Table3.Id WHERE Table.Active = 1

Query-2 (with standard sub-query):

SELECT City, State, (SELECT Name FROM Table2 WHERE Id = Table1.EmpId) AS Name, Code,
(SELECT ClassName FROM Table3 WHERE Id = Table1.ClassId) AS ClassName FROM Table1 WHERE Active = 1

For this task, the first approach, with

You May Also Find These Documents Helpful

  • Satisfactory Essays

    PT2520 Week 3 Forum

    • 190 Words
    • 1 Page

    Normalization is a very important database design that you should not assume that the highest level of normalization is always the most desirable. The higher the normal form is the more joins are required to produce a specified output and the more slowly the database system responds to end-user demands. A successful design must also consider end-user demand for fast performance you will occasionally be expected to de-normalize some portions of a database design in order to meet performance requirements. De-normalization produces a lower normal form.…

    • 190 Words
    • 1 Page
    Satisfactory Essays
  • Powerful Essays

    Nt1310 Unit 1

    • 4209 Words
    • 17 Pages

    It parses and also executes the statement Displays the execution plan for the select statement automatically…

    • 4209 Words
    • 17 Pages
    Powerful Essays
  • Good Essays

    1. SELECT e.ename, e.deptno, d.dname, d.deptno FROM dept d LEFT OUTER JOIN emp e ON d.deptno = e.deptno ORDER BY d.deptno; SQL> SELECT e.ename, e.deptno, d.dname, d.deptno 2 FROM dept d 3 LEFT OUTER JOIN emp e 4 ON d.deptno = e.deptno 5 ORDER BY d.deptno; ENAME DEPTNO DNAME DEPTNO ---------- ---------- -------------- ----------…

    • 1542 Words
    • 7 Pages
    Good Essays
  • Satisfactory Essays

    ProjectID where Projects. HourCapAmount <>0 group by Projects. ProjectName, Projects. HourCapAmount b.) Project Hour Details select Projects. ProjectName, concat(Employees.…

    • 485 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    3. Why do you think cross joins are allowed as a legitimate join? What uses can you see for joins?…

    • 337 Words
    • 1 Page
    Satisfactory Essays
  • Satisfactory Essays

    The main reason is to cut down the amount of information that the database is searching for. This also aids in increased performance. If cross joins were not allowed then the program would return much more information that is needed resulting in increased time to sort the data. This is example is two- fold; the database would work harder to retrieve information and the person looking at the returned information would have to work harder to sort it. I could imagine persons that are making inquiries across a WAN connection via a VPN making an inquiry and needing that specific data quickly. If cross joins were not allowed then can one imagine how much data is being sent across the internet or how much bandwidth is being used? In addition to that idea what about the integrity of data that any given person has access too? Cross joins allow for increased productivity, less strain on the database resources and security filtering of…

    • 434 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 6

    • 360 Words
    • 2 Pages

    8) The FROM keyword is used in a SELECT clause to determine which columns are included in the query.…

    • 360 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Unit 18 - Database P1, M1

    • 995 Words
    • 4 Pages

    A query is use for searching some specific record. It allow user to set conditions and then search the record which match the conditions. User also is able to make the search mix up with other table and sort the order of the result.…

    • 995 Words
    • 4 Pages
    Powerful Essays
  • Satisfactory Essays

    Extract the First and Last Name, Address, City, State, Zip Code and Phone Number of each senior on the database. Sort by Last Name, then First Name (1 sort).…

    • 325 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Assignment Homework

    • 1073 Words
    • 5 Pages

    2. Write queries that performs a join, a subquery, a correlated subquery using the student, enrollment, grade, and zipcode tables. Execute each query to show that it produces the same results. (15 pts)…

    • 1073 Words
    • 5 Pages
    Powerful Essays
  • Satisfactory Essays

    References: Pratt, P. J., & Last, M. Z. (2006). A Guide to MySQL . Boston, Massachusetts: Course Technology.…

    • 677 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    table based on data in another table. They generally are used when tables have some kind…

    • 1885 Words
    • 8 Pages
    Good Essays
  • Satisfactory Essays

    Database Normalisation

    • 2088 Words
    • 9 Pages

    A relationship between two tables, where a single row in one table is linked to a single row in another table.…

    • 2088 Words
    • 9 Pages
    Satisfactory Essays
  • Good Essays

    Database Normalization

    • 344 Words
    • 2 Pages

    For instance, in the sample table above, the sales contact list contain various field names to gather information about different product suppliers. Each field is unique and precisely asks for the most critical or essential information. Also, the table does not describe what type of products each company sales. Instead, the table only states the industry type because it does not want to limit the product search abilities to the company name only. Also, listing each product name within the same table can be confusing. By contrast, the table is split and related to other tables (Product Description, Orders, etc) using its Primary/Foreign keys to minimize data redundancy. The Product Description table contains a list of different items which are supplied by different companies. The order table pulls information from the Sales Contact, Product Information, etc. The main goal is to precisely identify and track each manufacturer…

    • 344 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    Data Base Queries

    • 2279 Words
    • 10 Pages

    "Group Functions" also known as "Multiple-Row Functions". They operates on set of rows to give one result per group. These set may be the whole table or the table split into groups.…

    • 2279 Words
    • 10 Pages
    Powerful Essays

Related Topics