What is the special operator used to check whether a subquery returns any rows?

You can include a subquery in a WHERE or HAVING clause by using a basic or quantified comparison, the IN keyword, or the EXISTS keyword.

Basic comparisons

You can use a subquery before or after any of the comparison operators. The subquery can return only one row. It can return multiple values for the row if the equal or not equal operators are used. SQL compares each value from the subquery row with the corresponding value on the other side of the comparison operator. For example, suppose that you want to find the employee numbers, names, and salaries for employees whose education level is higher than the average education level throughout the company.

     SELECT EMPNO, LASTNAME, SALARY
     FROM CORPDATA.EMPLOYEE
     WHERE EDLEVEL >
        (SELECT AVG(EDLEVEL)
           FROM CORPDATA.EMPLOYEE)

SQL first evaluates the subquery and then substitutes the result in the WHERE clause of the SELECT statement. In this example, the result is the company-wide average educational level. Besides returning a single row, a subquery can return no rows. If it does, the result of the compare is unknown.

Quantified comparisons (ALL, ANY, and SOME)

You can use a subquery after a comparison operator followed by the keyword ALL, ANY, or SOME. When used in this way, the subquery can return zero, one, or many rows, including null values. You can use ALL, ANY, and SOME in the following ways:

  • Use ALL to indicate that the value you supplied must compare in the indicated way to ALL the rows the subquery returns. For example, suppose you use the greater-than comparison operator with ALL:
    ... WHERE expression > ALL (subquery)

    To satisfy this WHERE clause, the value of the expression must be greater than the result for each of the rows (that is, greater than the highest value) returned by the subquery. If the subquery returns an empty set (that is, no rows were selected), the condition is satisfied.

  • Use ANY or SOME to indicate that the value you supplied must compare in the indicated way to at least one of the rows the subquery returns. For example, suppose you use the greater-than comparison operator with ANY:
    ... WHERE expression > ANY (subquery)

    To satisfy this WHERE clause, the value in the expression must be greater than at least one of the rows (that is, greater than the lowest value) returned by the subquery. If what the subquery returns is the empty set, the condition is not satisfied.

Note: The results when a subquery returns one or more null values may surprise you, unless you are familiar with formal logic.

IN keyword

You can use IN to say that the value in the expression must be among the rows returned by the subquery. Using IN is equivalent to using =ANY or =SOME. Using ANY and SOME were previously described. You can also use the IN keyword with the NOT keyword in order to select rows when the value is not among the rows returned by the subquery. For example, you can use:

... WHERE WORKDEPT NOT IN (SELECT …)

EXISTS keyword

In the subqueries presented so far, SQL evaluates the subquery and uses the result as part of the WHERE clause of the outer-level SELECT. In contrast, when you use the keyword EXISTS, SQL checks whether the subquery returns one or more rows. If it does, the condition is satisfied. If it returns no rows, the condition is not satisfied. For example:

     SELECT EMPNO,LASTNAME
     FROM CORPDATA.EMPLOYEE
     WHERE EXISTS
        (SELECT *
           FROM CORPDATA.PROJECT
           WHERE PRSTDATE > '1982-01-01');

In the example, the search condition is true if any project represented in the CORPDATA.PROJECT table has an estimated start date that is later than January 1, 1982. This example does not show the full power of EXISTS, because the result is always the same for every row examined for the outer-level SELECT. As a consequence, either every row appears in the results, or none appear. In a more powerful example, the subquery itself would be correlated, and change from row to row.

As shown in the example, you do not need to specify column names in the select-list of the subquery of an EXISTS clause. Instead, you should code SELECT *.

You can also use the EXISTS keyword with the NOT keyword in order to select rows when the data or condition you specify does not exist. You can use the following:

A database language enables the user to perform complex queries designed to transform the raw data into useful information.

  • A. 

    True

  • B. 

    False

  • 2. 

    SQL is considered difficult to learn; its command set has a vocabulary of more than 300 words

    • A. 

      True

    • B. 

      False

  • 3. 

    The COMMIT command does not permanently save all changes. In order to do that, you must use SAVE.

    • A. 

      True

    • B. 

      False

  • 4. 

    All SQL commands must be issued on a single line.

    • A. 

      True

    • B. 

      False

  • 5. 

    Although SQL commands can be grouped together on a single line, complex command sequences are best shown on separate lines, with space between the SQL command and the command’s components.

    • A. 

      True

    • B. 

      False

  • 6. 

    An alias cannot be used when a table is required to be joined to itself in a recursive query.

    • A. 

      True

    • B. 

      False

  • 7. 

    Comparison operators cannot be used to place restrictions on character-based attributes.

    • A. 

      True

    • B. 

      False

  • 8. 

    11. String comparisons are made from left to right.

    • A. 

      True

    • B. 

      False

  • 9. 

    13. SQL allows the use of logical restrictions on its inquiries such as OR, AND, and NOT.

    • A. 

      True

    • B. 

      False

  • 10. 

    14. You cannot insert a row containing a null attribute value using SQL.

    • A. 

      True

    • B. 

      False

  • 11. 

    16. The conditional LIKE must be used in conjunction with wildcard characters.

    • A. 

      True

    • B. 

      False

    • C. 

      Option 3

    • D. 

      Option 4

  • 12. 

    19. The COUNT function is designed to tally the number of non-null "values" of an attribute, and is often used in conjunction with the DISTINCT clause.

    • A. 

      True

    • B. 

      False

    • C. 

      Option 3

    • D. 

      Option 4

  • 13. 

    21. The SQL data manipulation command HAVING:

    • A. 

      restricts the selection of rows based on a conditional expression.

    • B. 

      restricts the selection of grouped rows based on a condition.

    • C. 

      modifies an attribute’s values in one or more table’s rows.

    • D. 

      groups the selected rows based on one or more attributes.

  • 14. 

    22. The SQL command that allows a user to permanently save data changes is _____.

    • A. 

      INSERT

    • B. 

      SELECT

    • C. 

      COMMIT

    • D. 

      UPDATE

  • 15. 

    23. The _____ command defines a default value for a column when no value is given.

    • A. 

      CHECK

    • B. 

      UNIQUE

    • C. 

      NOT NULL

    • D. 

      DEFAULT

  • 16. 

    24. The _____ command restricts the selection of grouped rows based on a condition.

    • A. 

      DISPLAY

    • B. 

      HAVING

    • C. 

      FROM

    • D. 

      CONVERT

  • 17. 

    25. A(n) _____ query specifies which data should be retrieved and how it should be filtered, aggregated, and displayed.

    • A. 

      INSERT

    • B. 

      SELECT

    • C. 

      COMMIT

    • D. 

      UPDATE

  • 18. 

    26. A(n) _____ is an alternate name given to a column or table in any SQL statement.

    • A. 

      alias

    • B. 

      data type

    • C. 

      stored function

    • D. 

      trigger

  • 19. 

    30. A(n) _____ join will select only the rows with matching values in the common attribute(s).

    • A. 

      Natural

    • B. 

      Outer

    • C. 

      Full

    • D. 

      Cross

  • 20. 

    36. Which comparison operator indicates a value is not equal?

    • A. 

      <

    • B. 

      <=

    • C. 

      >=

    • D. 

      <>

  • 21. 

    38. The special operator used to check whether an attribute value is within a range of values is _____.

    • A. 

      BETWEEN

    • B. 

      IS NULL

    • C. 

      LIKE 

    • D. 

      IN

  • 22. 

    39. The special operator used to check whether an attribute value ro string pattern is _____.

    • A. 

      BETWEEN

    • B. 

      IS NULL

    • C. 

      LIKE

    • D. 

      IN

  • 23. 

    40. The SQL aggregate function that gives the number of rows containing non-null values for a given column is _____.

    • A. 

      COUNT

    • B. 

      MIN

    • C. 

      MAX

    • D. 

      SUM

  • 24. 

    41. A(n) _____ is a query that is embedded (or nested) inside another query.

    • A. 

      Alias

    • B. 

      operator

    • C. 

      Subquery

    • D. 

      View

  • 25. 

    43. The special operator used to check whether a subquery returns any rows is _____.

    • A. 

      BETWEEN

    • B. 

      EXISTS

    • C. 

      LIKE

    • D. 

      IN

  • Below is a Relational Database Trivia Quiz! It is designed to help you understand how databases holding the same information are structured and maintained. Do you think that you have what it takes to tackle it based on what you...

    Questions: 9  |  Attempts: 8715   |  Last updated: Mar 21, 2022

    • Sample Question

      Which of the following makes it possible for entities toshare a relationship?

      What is the special operator used to check whether a subquery returns any rows?

      Multi-valued attributes

      A foreign key

      A common attribute

      The same number of attributes

    Ever wondered how much you know about DBMS, or the Database Management System? For this quiz, you should know what is responsible for authorizing access to the databases and obtaining software and hardware services, what is a...

    Questions: 31  |  Attempts: 7781   |  Last updated: Mar 22, 2022

    • Sample Question

      -------------------------------responsible for authorizing access to the database, for coordinating and monitoring its use, acquiring software, and hardware resources, controlling its use, andmonitoring efficiency of operations.

      What is the special operator used to check if an attribute is in a range?

      IST210Final.

      Which operator can you use to test whether one or more rows are returned by a subquery?

      The EXISTS Operator. The EXISTS operator check the number of rows returned by a subquery. If the subquery contains one or more rows, then the result is true and a row is placed in the result table; otherwise, the result is false and no row is added to the result table.

      Which operator is used to check whether an attribute value is null?

      The IS NULL operator is used to test for empty values (NULL values).

      What is an SQL operator to check whether an attribute value matches any value within the given list of values?

      The IN operator allows you to determine if a value matches any value in a list of values. Here's the syntax of the IN operator: value IN (value1, value2, value3,...) The IN operator returns 1 (true) if the value equals any value in the list ( value1 , value2 , value3 ,…).