Write an SQL query to retrieve the supplier number and supplier names from the supplier table, where the supplier name starts with the letter ‘R’.
The Correct Answer and Explanation is:
✅ SQL Query:
SELECT supplier_number, supplier_name
FROM supplier
WHERE supplier_name LIKE 'R%';
🧠 Explanation (300+ words):
This SQL query retrieves data from a table named supplier, specifically targeting records where the supplier_name starts with the letter ‘R’. Let’s break it down step by step to understand how it works:
1. SELECT Clause:
SELECT supplier_number, supplier_name
The SELECT statement is used to specify which columns you want to retrieve from the database. In this case, we are selecting:
supplier_number: A unique identifier for each supplier.supplier_name: The name of the supplier.
These columns will be shown in the final result set.
2. FROM Clause:
FROM supplier
This tells the SQL engine which table to pull the data from. Here, it is the supplier table.
3. WHERE Clause:
WHERE supplier_name LIKE 'R%';
The WHERE clause is used to filter rows based on specific conditions. In this query, we use it to check if the supplier_name starts with the letter ‘R’.
4. LIKE Operator:
The LIKE keyword is used for pattern matching in SQL. The pattern 'R%' breaks down as follows:
'R': The name must start with the letter ‘R’.'%': This is a wildcard that represents zero or more characters following ‘R’. So, it will match names like ‘Robert’, ‘Riverside’, ‘R-Tech’, etc.
5. Why Use LIKE Instead of =?
Using = would require an exact match. For example:
WHERE supplier_name = 'Robert';
This would only return suppliers named exactly ‘Robert’. But we want all names starting with ‘R’, so we use LIKE 'R%'.
📝 Summary:
The query:
SELECT supplier_number, supplier_name FROM supplier WHERE supplier_name LIKE 'R%';
is the correct way to retrieve all suppliers whose names begin with ‘R’, returning their numbers and names efficiently using pattern matching.