SQL LIKE Operator and Wildcards

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which SQL clause is used with the LIKE operator to specify a search pattern?

  • ORDER BY
  • GROUP BY
  • WHERE (correct)
  • HAVING

Which wildcard character in SQL LIKE is used to represent zero or more characters?

  • []
  • #
  • % (correct)
  • _

Which SQL LIKE pattern would you use to find all names that start with 'A'?

  • '_A'
  • '%A'
  • 'A%' (correct)
  • '[^A]%'
  • '[A]%'

Which SQL LIKE pattern would you use to find all cities containing 'es'?

<p>'%es%' (B)</p> Signup and view all the answers

To find cities that have six letters with the second letter being 'o', and ending in 'ondon', what LIKE pattern would you use?

<p>'_ondon' (A)</p> Signup and view all the answers

If you want to find all last names ending with 'son', how would you structure your SQL LIKE clause?

<p>WHERE last_name LIKE '%son' (A)</p> Signup and view all the answers

What is the main purpose of the SQL IN operator?

<p>To filter records that match any value in a list. (C)</p> Signup and view all the answers

Which SQL statement correctly filters employees in the 'HR', 'IT', or 'Sales' departments using the IN operator?

<p>SELECT * FROM employees WHERE department IN ('HR', 'IT', 'Sales') (A)</p> Signup and view all the answers

How can the SQL IN operator be used with subqueries?

<p>To filter results based on the result of another query. (B)</p> Signup and view all the answers

What is the purpose of the SQL BETWEEN operator?

<p>To filter values within a specific range. (B)</p> Signup and view all the answers

To find orders placed in 2024, which SQL statement using the BETWEEN operator would you use?

<p>SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31' (D)</p> Signup and view all the answers

How would you select products with prices between 100 and 500 (inclusive) using the SQL BETWEEN operator?

<p>SELECT * FROM products WHERE price BETWEEN 100 AND 500 (D)</p> Signup and view all the answers

If you're searching customer_feedback table for feedback where customer name contains 'a', what SQL query would you use?

<p>SELECT * FROM customer_feedback WHERE customer_name LIKE '%a%' (C)</p> Signup and view all the answers

To find all feedback comments that begin with the word 'Good', which SQL query would you use?

<p>SELECT * FROM customer_feedback WHERE feedback LIKE 'Good%' (C)</p> Signup and view all the answers

Which SQL query would you use to find feedback where the comment ends with the word 'service'?

<p>SELECT * FROM customer_feedback WHERE feedback LIKE '%service' (C)</p> Signup and view all the answers

How do you select customer feedback with ratings of 3, 4, or 5?

<p>All of the above (D)</p> Signup and view all the answers

Given a table customer_feedback, what SQL query would retrieve feedback provided specifically on '2024-03-01', '2024-03-05', and '2024-03-10'?

<p>SELECT * FROM customer_feedback WHERE date_given IN ('2024-03-01', '2024-03-05', '2024-03-10') (D)</p> Signup and view all the answers

Which of the following is NOT a valid use case for the BETWEEN operator?

<p>Selecting records where a value matches one of several discrete, specific values. (C)</p> Signup and view all the answers

You want to retrieve records of all orders placed in the first half of 2024. Which BETWEEN clause would achieve this?

<p>WHERE order_date BETWEEN '2024-01-01' AND '2024-06-30' (C)</p> Signup and view all the answers

Given a table products with a price column, what SQL statement would retrieve items with prices of exactly 25, 50, or 75, using the most efficient approach?

<p>SELECT * FROM products WHERE price IN (25, 50, 75) (D)</p> Signup and view all the answers

Signup and view all the answers

Flashcards

SQL LIKE operator

Used in a WHERE clause to search for a specified pattern in a column.

SQL Wildcard: %

Matches zero or more characters.

SQL Wildcard: _

Matches exactly one character.

SQL Wildcard: []

Matches any single character inside brackets.

Signup and view all the flashcards

SQL Wildcard: [^]

Matches any character NOT in brackets.

Signup and view all the flashcards

SQL IN operator

Filter records that match any value in a list; shorthand for multiple OR conditions.

Signup and view all the flashcards

SQL BETWEEN operator

Filter values within a specific range (numbers, dates, or text).

Signup and view all the flashcards

Study Notes

SQL LIKE Operator

  • Implemented in a WHERE clause to find a specific pattern in a column
  • Syntax includes SELECT column1, column2,... FROM table_name WHERE columnN LIKE pattern;

Wildcards Used with LIKE

  • % matches zero or more characters, such as 'M%' matching "Mark" or "Maria"
  • _ matches exactly one character, such as 'M_' matching "Ma" or "Mo"
  • [ ] matches any single character inside brackets; '[AB]%' matches names beginning with A or B
  • [^ ] matches any character NOT in brackets;'[^M]%' matches names not beginning with M

LIKE Pattern Examples

  • 'a%' finds entries starting with “a”
  • '%a' finds entries ending with "a"
  • '%or%' finds entries containing "or" anywhere
  • '_r%' finds entries where the second character is "r"
  • 'a_%' finds entries starting with "a" and has at least 2 characters
  • 'a%o' finds entries that start with "a" and end with "o"

LIKE Query Descriptions

  • SELECT * FROM Customers WHERE City LIKE 'ber%'; finds cities beginning with "ber" such as "Berlin"
  • SELECT * FROM Customers WHERE City LIKE '%es%'; finds cities containing "es" such as "Brest" or "Estonia"
  • SELECT * FROM Customers WHERE City LIKE '_ondon'; finds 6-letter cities with the second letter "o" and ending in "ondon" such as "London"
  • SELECT * FROM Customers WHERE City LIKE 'L_n_on'; finds cities such as "London" or "Lennon" using the pattern L + any character + n + any character + on

Practice Modifications (LIKE)

  • SELECT * FROM employees WHERE last_name LIKE '%son'; retrieves names ending in "son"
  • SELECT * FROM customers WHERE email LIKE '%gmail.com'; searches for emails with "gmail"

SQL IN Operator

  • Filters records matching any value in a list
  • Acts as shorthand for multiple OR conditions
  • Syntax includes SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2,...);
  • SELECT * FROM employees WHERE department IN ('HR', 'IT', 'Sales'); to filter employees in specific departments
  • The above is equivalent to SELECT * FROM employees WHERE department = 'HR' OR department = 'IT' OR department = 'Sales';
  • SELECT * FROM students WHERE grade IN ('A', 'B', 'C'); locates students with grades A, B, or C
  • SELECT * FROM orders WHERE customer_id IN (101, 102, 105); locates orders by customers 101, 102, and 105
  • SELECT * FROM products WHERE price IN (100, 200, 500); locates products costing 100, 200, or 500

IN with Subqueries

  • SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'New York');

SQL BETWEEN Operator

  • Filters values within a defined range
  • Works with numbers, dates, or text (alphabetically)
  • SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31'; finds orders placed in 2024
  • SELECT * FROM products WHERE price BETWEEN 100 AND 500; finds products with prices between 100 and 500

Hands-On SQL Activity: Feedback Table

  • Create a customer feedback table with the following statement CREATE TABLE customer_feedback ( id INT PRIMARY KEY AUTO_INCREMENT, customer_name VARCHAR(50), feedback TEXT, rating INT, date_given DATE );
  • Insert at least 5 entries of sample data
  • Perform queries to retrieve certain data

Example Queries

  • SELECT * FROM customer_feedback WHERE customer_name LIKE '%a%'; finds feedback where the name contains "a"
  • SELECT * FROM customer_feedback WHERE feedback LIKE 'Good%'; finds feedback where the comment starts with "Good"
  • SELECT * FROM customer_feedback WHERE feedback LIKE '%service'; finds feedback where the comment ends with "service"
  • SELECT * FROM customer_feedback WHERE rating IN (3, 4, 5); finds feedback with a rating of 3, 4, or 5
  • SELECT * FROM customer_feedback WHERE customer_name IN ('John', 'Emma','Michael'); finds feedback from "John", "Emma", or "Michael"
  • SELECT * FROM customer_feedback WHERE date_given IN ('2024-03-01', '2024-03-05', '2024-03-10'); finds feedback given on specific dates

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

SQL Basics Quiz
10 questions

SQL Basics Quiz

HaleRetinalite2094 avatar
HaleRetinalite2094
SQL Fundamentals Quiz
18 questions

SQL Fundamentals Quiz

EntrancingBaritoneSaxophone avatar
EntrancingBaritoneSaxophone
ClassicModels SQL Exercises
26 questions
Use Quizgecko on...
Browser
Browser