Explain Codes LogoExplain Codes Logo

How to use a LIKE with a JOIN in SQL?

sql
join
performance
best-practices
Anton ShumikhinbyAnton Shumikhin·Oct 3, 2024
TLDR

To use LIKE within a JOIN in SQL, you express the LIKE condition within the JOIN clause. Here's a simple illustration:

SELECT * FROM first_table ft JOIN second_table st ON ft.name LIKE '%' || st.partial_name || '%';

This matches the name field from the first_table to the partial_name field from the second_table, using wildcard %, which signifies any string of zero or more characters.

Optimizing for Performance and Consistency

Constructing SQL queries with performance and accuracy in mind requires keen attention to case sensitivity, the use of wildcards and JOIN separation.

Use SQL's UPPER function for case-insensitive searching when the database collation is case-sensitive. This results in a more consistent outcome.

SELECT * FROM first_table ft JOIN second_table st ON UPPER(ft.name) LIKE UPPER('%' || st.partial_name || '%');

Remember consistency matters!

Wildcard Placement: A Cardinal Rule

Wildcards (% and _) are quite handy, but placement can greatly affect performance. Regularly, avoiding a leading wildcard can prevent a full table scan, optimizing query performance.

-- Discouraged: This leads to full table scan, potentially slow ON ft.name LIKE '%' || st.partial_name; -- Recommended: Energetic and faster, sans leading wildcard ON ft.name LIKE st.partial_name || '%';

To confirm this theory, you can execute EXPLAIN PLAN. It doesn't bite! 😜

De-Coupling Join and Filter Logic

Keeping joining conditions and filtering logic separated promotes code readability and likely better optimization during execution.

SELECT * FROM first_table ft JOIN second_table st ON ft.id = st.ft_id -- join condition WHERE st.description LIKE '%search_term%'; -- filter condition

Handling Various JOIN Scenarios

From inner to outer, each JOIN type plays a pivotal role in discarding or preserving records from table relations.

The Intersection: INNER JOIN

If you want matches that follow a particular pattern across tables, INNER JOIN is key.

SELECT * FROM authors a INNER JOIN books b ON a.name LIKE '%' || b.author_name_fragment || '%';

This tells you all the author names matching a fragment from the books table.

The Inclusive: LEFT JOIN

To retain all the primary table records, even when there isn't a match, LEFT JOIN works like your favorite elastic waistband jeans.

SELECT * FROM authors a LEFT JOIN books b ON a.name LIKE '%' || b.author_name_fragment || '%';

It's a LEFT JOIN, hence it leans left!

The Speedster: INSTR

If LIKE is acting lethargic, switching to INSTR might provide a jolt to your query's performance.

SELECT * FROM first_table ft JOIN second_table st ON INSTR(ft.name, st.partial_name) > 0;

The Booster: Precalculated Columns

When performance is key, precalculate columns for an extra boost, especially in complex scenarios like storing the length of a column's values.

ALTER TABLE ft ADD (name_length AS LENGTH(name)); SELECT * FROM first_table ft JOIN another_table at ON ft.name_length = at.some_length;

Yes, it feels like cheating, but it's not. Cleavage is fair in love, war, and SQL performance!