Write an SQL query to retrieve the supplier number and supplier names from the supplier table

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 :

To retrieve the supplier number and supplier names from the supplier table where the supplier name starts with the letter ‘R’, you can use the following SQL query:

SELECT supplier_number, supplier_name
FROM supplier
WHERE supplier_name LIKE 'R%';

Explanation:

  1. SELECT supplier_number, supplier_name:
  • The SELECT statement specifies the columns to retrieve from the database. Here, supplier_number and supplier_name are selected.
  1. FROM supplier:
  • The FROM clause indicates the table from which to retrieve the data. In this case, it’s the supplier table.
  1. WHERE supplier_name LIKE ‘R%’:
  • The WHERE clause filters the rows based on specified conditions.
  • The LIKE operator is used to search for a specified pattern in a column.
  • The pattern 'R%' means the supplier name must start with the letter ‘R’.
  • The % symbol is a wildcard character that matches any sequence of characters (including no characters). So, 'R%' matches any string that begins with ‘R’.

How the Query Works:

  • The SELECT statement retrieves data from the specified columns.
  • The FROM clause identifies the source table for the data.
  • The WHERE clause applies a filter to include only rows where the supplier_name starts with ‘R’.
  • The LIKE 'R%' condition ensures that only supplier names beginning with ‘R’ are selected.

Example:

Consider a supplier table with the following data:

supplier_numbersupplier_name
1Robert
2Alice
3Rachel
4Bob
5Rick

Applying the query:

SELECT supplier_number, supplier_name
FROM supplier
WHERE supplier_name LIKE 'R%';

Would return:

supplier_numbersupplier_name
1Robert
3Rachel
5Rick

This result includes only the suppliers whose names start with ‘R’.

Additional Notes:

  • SQL is case-insensitive by default in many databases. Therefore, 'R%' will match names starting with both ‘R’ and ‘r’. If case sensitivity is required, you may need to use a case-sensitive collation or function, depending on your database system.
  • Ensure that the column names (supplier_number and supplier_name) and the table name (supplier) match exactly those in your database schema.
  • If your database uses different naming conventions (e.g., snake_case), adjust the column and table names accordingly.

This query efficiently retrieves the desired supplier information based on the specified condition.

Scroll to Top