Basic string searches

Question

How can you produce a list of all facilities with the word 'Tennis' in their name?

Expected Results

facidnamemembercostguestcostinitialoutlaymonthlymaintenance
0Tennis Court 152510000200
1Tennis Court 25258000200
3Table Tennis0532010

Your Answer

Your Results

Loading database...

Answers and Discussion

select *
	from cd.facilities 
	where 
		name like '%Tennis%';

SQL's LIKE operator provides simple pattern matching on strings. It's pretty much universally implemented, and is nice and simple to use - it just takes a string with the % character matching any string, and _ matching any single character. In this case, we're looking for names containing the word 'Tennis', so putting a % on either side fits the bill.

There's other ways to accomplish this task: Postgres supports regular expressions with the ~ operator, for example. Use whatever makes you feel comfortable, but do be aware that the LIKE operator is much more portable between systems.