w3resource

AdventureWorks Database: Persons whose last name begins with 'L'

SQL Query - AdventureWorks: Exercise-11 with Solution

11. From the following tables write a query in SQL to find the persons whose last name starts with letter 'L'. Return BusinessEntityID, FirstName, LastName, and PhoneNumber. Sort the result on lastname and firstname.

Sample table: Person.PersonPhone


Click to view Full table

Sample table: Person.Person


Click to view Full table

Sample Solution:

SELECT p.BusinessEntityID, FirstName, LastName, PhoneNumber AS Person_Phone  
FROM Person.Person AS p  
JOIN Person.PersonPhone AS ph 
ON p.BusinessEntityID  = ph.BusinessEntityID  
WHERE LastName LIKE 'L%'  
ORDER BY LastName, FirstName;

Sample Output:

businessentityid|firstname  |lastname       |person_phone       |
----------------+-----------+---------------+-------------------+
            5527|Aaron      |Lal            |605-555-0159       |
            5268|Adam       |Lal            |513-555-0110       |
           12539|Alejandro  |Lal            |1 (11) 500 555-0117|
           19786|Alicia     |Lal            |1 (11) 500 555-0161|
           12004|Alisha     |Lal            |1 (11) 500 555-0119|
           16649|Alison     |Lal            |1 (11) 500 555-0177|
            5005|Alvin      |Lal            |1 (11) 500 555-0168|
            5070|Andres     |Lal            |1 (11) 500 555-0127|
           10416|Arturo     |Lal            |638-555-0164       |
            8951|Ashlee     |Lal            |1 (11) 500 555-0148|
            6283|Austin     |Lal            |541-555-0141       |
           11600|Barbara    |Lal            |1 (11) 500 555-0176|
            6744|Benjamin   |Lal            |1 (11) 500 555-0148|
           17275|Bethany    |Lal            |1 (11) 500 555-0196|
            3694|Bonnie     |Lal            |1 (11) 500 555-0191|
            9390|Brad       |Lal            |463-555-0111       |
           20292|Bradley    |Lal            |1 (11) 500 555-0124|
		   ...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Expression used in group by.
Next: Calculate running totals and subtotals.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

T-SQL Cast versus Convert?

CONVERT is SQL Server specific, CAST is ANSI.

CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

Ref : https://bit.ly/3VKT5M8