w3resource

SQL SOME Operator

SOME Operator

SOME compare a value to each value in a list or results from a query and evaluate to true if the result of an inner query contains at least one row. SOME must match at least one row in the subquery and must be preceded by comparison operators. Suppose using greater than ( >) with SOME means greater than at least one value.

Syntax:

SELECT [column_name... | expression1 ]
FROM [table_name]
WHERE expression2 comparison_operator {ALL | ANY | SOME} ( subquery )

Parameters:

Name Description
column_name Name of the column of the table.
expression1 Expression made up of a single constant, variable, scalar function, or column name and can also be the pieces of a SQL query that compare values against other values or perform arithmetic calculations.
table_name Name of the table.
WHERE expression2 Compares a scalar expression until a match is found for SOME operator. One or more rows must match the expression to return a Boolean TRUE value for the SOME operator.
comparison_operator Compares the expression to the subquery. The comparison must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

Pictorical Presentation: SQL SOME Operator

SQL SOME Operator

DBMS Support: SOME Operator

DBMS Command
MySQL Supported
PostgreSQL Supported
SQL Server Supported
Oracle Supported

Example: SQL SOME Operator

Sample table: agents


Sample table: customer


To get 'agent_code', 'agent_name', 'working_area', 'commission' from 'agents' table with following conditions -

1. 'agent_code' should be within some 'agent_code' from 'customer' table, which satisfies the condition bellow:

  a) 'cust_country' in the 'customer' table must be 'UK',

the following SQL statement can be used :

SQL Code:


SELECT agent_code, agent_name, working_area, commission
-- Selecting specific columns: agent_code, agent_name, working_area, and commission
FROM agents
-- From the table named "agents"
WHERE agent_code = SOME (
    SELECT agent_code FROM customer
    -- Selecting agent_code from the "customer" table
    WHERE cust_country = 'UK'
    -- Where the value in the column "cust_country" is 'UK'
);
-- Where the value in the column "agent_code" is equal to any value returned by the subquery

Explanation:

  • SELECT agent_code, agent_name, working_area, commission: This specifies that we want to retrieve specific columns (agent_code, agent_name, working_area, and commission) from the table.

  • FROM agents: This indicates the table from which we want to retrieve the data. In this case, the table is named "agents".

  • WHERE agent_code = SOME (...): This is a conditional clause that filters the rows returned by the query. It specifies that only rows where the value in the column "agent_code" is equal to any value returned by the subquery should be included in the result set.

  • The subquery inside SOME (...) selects agent_code from the "customer" table where cust_country is 'UK'.

  • SOME (...): This operator is used to compare a value with any value returned by a subquery and returns true if the comparison holds true for at least one value.

Output:

AGENT_CODE AGENT_NAME           WORKING_AREA         COMMISSION
---------- -------------------- -------------------- ----------
A009       Benjamin             Hampshair                   .11
A003       Alex                 London                      .13
A006       McDen                London                      .15

See our Model Database

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: ALL
Next: EXISTS



Follow us on Facebook and Twitter for latest update.