Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 24, 2024
In SQL, a common goal is to query data efficiently. For instance, selecting a single row based on multiple criteria from the same column can help us filter data against complex conditions without overstepping column boundaries. As a result, we can easily query for precise data.
In this tutorial, we’ll use examples to provide a practical understanding of single-column filtering in MySQL, PostgreSQL, and SQL Server.
In particular, let’s work with the table Course in the Baeldung University Schema.
In data filtering, checking multiple conditions within a single column is common. For instance, we may need to use a query that selects a row with a column containing specific keywords or matching multiple values.
SQL provides the WHERE clause which can help us in data filtering. In addition, we can combine the universal WHERE with operators when working with multiple criteria for the same column:
If multiple rows meet the criteria, we can specify the filtering conditions along with features like ORDER BY to order the results and LIMIT or TOP to restrict the output to a single row.
So, let’s utilize the Course table which lists course details such as id, name, textbook, credits, is_active, and department_id. Specifically, we aim to select a single row where the textbook column matches multiple criteria.
MySQL provides a flexible way to query data using the WHERE clause in combination with logical operators:
> SELECT *
FROM Course
WHERE textbook LIKE '%Numerical%' AND textbook LIKE '%Optimization%'
ORDER BY id
LIMIT 1;
+-------+----------------------+------------------------+---------+-----------+---------------+
| id | name | textbook | credits | is_active | department_id |
+-------+----------------------+------------------------+---------+-----------+---------------+
| MA441 | Advanced Optimizaton | Numerical Optimization | 5 | No | 5 |
+-------+----------------------+------------------------+---------+-----------+---------------+
1 row in set (0.00 sec)
Here, we also use the LIKE operator for pattern matching:
So, this query scans the Course table and displays the row that meets both criteria for the given column.
The methods in this section, using the WHERE clause in combination with logical operators such as AND and LIKE, aren’t specific to MySQL. These techniques are supported in most SQL systems such as PostgreSQL and SQL Server.
PostgreSQL also supports the use of text pattern matching and logical operators.
For example, let’s construct a query using the OR operator:
> SELECT * FROM Course WHERE textbook LIKE '%Numerical%' OR textbook LIKE '%Optimization%';
id | name | textbook | credits | is_active | department_id
-------+-----------------------------+---------------------------------+---------+-----------+---------------
MA141 | Introduction to Optimizaton | An Introduction to Optimization | 7 | Yes | 5
MA241 | Convex Optimizaton | An Introduction to Optimization | 7 | No | 5
MA441 | Advanced Optimizaton | Numerical Optimization | 5 | No | 5
(3 rows)
Above, the query outputs three rows. It filters the rows where the textbook column contains either the substring Numerical or the substring Optimization.
However, we can modify the query to ensure we get a single row:
> SELECT *
FROM Course
WHERE textbook LIKE '%Numerical%' OR textbook LIKE '%Optimization%'
ORDER BY id
LIMIT 1;
id | name | textbook | credits | is_active | department_id
-------+-----------------------------+---------------------------------+---------+-----------+---------------
MA141 | Introduction to Optimizaton | An Introduction to Optimization | 7 | Yes | 5
(1 row)
This query selects a row from the Course table where the textbook matches either of the two criteria (Numerical or Optimization). It prioritizes rows based on the id column and returns only the top result.
Additionally, we can modify this query to use regular expressions:
> SELECT *
FROM Course
WHERE textbook ~* 'Numerical|Optimization'
ORDER BY id
LIMIT 1;
Let’s understand the modification textbook ~* ‘Numerical|Optimization’:
Rows that differ in case, like NUMERICAL optimization, would also match due to the case-insensitivity of ~*.
To be clear, ~* for case-insensitive regex is unique to PostgreSQL whereas the WHERE clause and logical operators are applicable in most SQL systems like PostgreSQL.
Lastly, let’s demonstrate selecting a single row based on multiple criteria from the textbook column in SQL Server:
> SELECT TOP 1 *
FROM Course
WHERE textbook LIKE 'Signals%Oppenheim'
ORDER BY id;
id | name | textbook | credits | is_active | department_id
-------+--------------------+----------------------------------+---------+-----------+---------------
EC121 | Signal and Systems | Signals and Systems by Oppenheim | 7 | Yes | 2
(1 row)
The query works in a manner similar to the previous examples:
The WHERE clause with LIKE is a universal SQL feature. However, the TOP clause is specific to SQL Server.
In this article, we explored how to select a single row based on multiple criteria from one column in SQL. To be specific, we covered MySQL, PostgreSQL, and SQL Server.
We leveraged logical operators, pattern matching, regular expressions, and query features like ORDER BY and LIMIT. Hence, we can now handle complex queries in different database environments and optimize data filtering.