Difference between revisions of "SELECT names"

From SQLZOO
Jump to: navigation, search
(Created page with "<div class="ref_section"> <table class='db_ref'> <tr><th>name</th><th>continent</th></tr> <tr><td>Afghanistan</td><td>Asia</td></tr> <tr><td>Albania</td><td>Europe</td></tr> <...")
 
Line 14: Line 14:
  
 
==Pattern Matching Strings==
 
==Pattern Matching Strings==
  <p>This  tutorial uses the '''LIKE''' operator to check names. We will be using the SELECT command on the table '''world''':</p>
+
<p>This  tutorial uses the '''LIKE''' operator to check names. We will be using the SELECT command on the table '''world''':</p>
 +
Some examples:
 +
*Countries that start with B
 +
<source lang='sql'>
 +
SELECT name FROM world
 +
WHERE name LIKE 'B%'
 +
</source>
 +
*Countries that end with 'stan'
 +
<source lang='sql'>
 +
SELECT name FROM world
 +
WHERE name LIKE '%stan'
 +
</source>
 +
*Countries that contain the letter z
 +
<source lang='sql'>
 +
SELECT name FROM world
 +
WHERE name LIKE '%z%'
 +
</source>
  
 
<div class="progress_panel"><div>
 
<div class="progress_panel"><div>

Revision as of 20:49, 20 November 2014

namecontinent
AfghanistanAsia
AlbaniaEurope
AlgeriaAfrica
AndorraEurope
AngolaAfrica
....

Pattern Matching Strings

This tutorial uses the LIKE operator to check names. We will be using the SELECT command on the table world:

Some examples:

  • Countries that start with B
SELECT name FROM world
WHERE name LIKE 'B%'
  • Countries that end with 'stan'
SELECT name FROM world
WHERE name LIKE '%stan'
  • Countries that contain the letter z
SELECT name FROM world
WHERE name LIKE '%z%'
Summary

The clause name LIKE 'B%' finds those countries that start with "B".

  • The % is a wild-card it can match any characters

Find the country that start with Y

SELECT name FROM world
  WHERE name LIKE 'F%'
SELECT name FROM world
  WHERE name LIKE 'Y%'