Basic SQL Query, I am newbie - sql

I just started my database and query class on Monday. We met on Monday and just went over the syllabus, and on Wednesday the network at school was down so we couldn't even do the power point lecture. Right now I am working on my first homework assignment and I am almost finished but I am having trouble on one question.
Here is is...
Write a SELECT statement that returns one column from the Customers table named FullName that joins the LastName and FirstName columns.
Format the columns with the last name, a comma, a space, and the first name like this:
Doe, John
Sort the result set by last name in ascending sequence.
Return only the contacts whose last name begins with letters from M to Z.
Here is what I have so far...
USE md0577283
SELECT FirstName,LastName
FROM Customers
ORDER BY LastName,FirstName
My question is how do I format is Lastname, FirstName like the professor wants and how do I only select names M-Z?
If someone could point me in the right direction I would greatly appreciate it.
Thank you.
PS With all do respect, I didn't ask for the answer I asked for a nudge in the right direction so why the down vote guys?

USE md0577283
SELECT LastName + ', ' + FirstName FullName
FROM Customers
WHERE LastName LIKE '[M-Z]%'
ORDER BY LastName,FirstName

You want to add two things: create an expression to return the name in the requested format
(LastName + ", " + FirstName as Name)
USe a "where clause" to filter what is returned: where LastName >= "M" and LastName <= "Z" perhaps.

Simply write like this.
If you want to get names from m to z.
SELECT LastName, FirstName
FROM Customers
WHERE FirstName between 'M%' and 'Z%'
ORDER BY LastName, FirstName
If you want to get names from m and z only.
SELECT LastName, FirstName
FROM Customers
WHERE FirstName LIKE 'M%' OR FirstName LIKE 'Z%'
ORDER BY LastName, FirstName

Related

What is the SQL to select all the records from a table where the "LastName" is alphabetically between (and including) "Hansen" and "Peterson"?

My current Statement is
SELECT * FROM Persons WHERE LastName >’Hansen’ AND LastName <’Peterson’
is there a better way to do this?
Some people said
SELECT FROM Persons WHERE LastName BETWEEN ‘Hansen’ AND ‘Peterson’
is more correct

Matching pattern in SQL

I have two tables and need to match all records by name. The problem is that in one table name format is FirstName LastName, in another table - LastName FirstName, and I cannot split into separate columns because some records might have few first names or last names, so I don't know where first or last name ends or starts.
Eg. in first table I have John Erick Smith and need to join all records from another table where the name is Smith John Erick.
Any solution in SQL?
I think you can use string functions to get the piece of string (in 'John Erick Smith' type column) after the last space as a surname and stick it to front. Then you could compare the strings. That is assuming you don't have spaces in surnames.
Here is MSDN article on how to do it.
DECLARE #string nvarchar(max) = 'FirstName SecondName Surname'
SELECT RIGHT(#string, NULLIF(charindex(' ', REVERSE(#string)),0)) + ' ' +
REVERSE(RIGHT(REVERSE (#string), len(#string) - NULLIF(charindex(' ', REVERSE(#string)),0)))
Returns:
Surname FirstName SecondName
Verify first if you still have other tables with "FirstName" and "LastName" that you can use instead of using the field with "FirstName LastName". Normally Oracle has this kind of tables use for persons/employees. You may have something like this.
But if the "LastName FirstName" uses "," (comma) in its data then you can do a substring to get the LastName from the FirstName.
Or another alternative is by using their IDs (eg. employee IDs) [if only available].

Search functionality for multiple fields using sql

I have a table that holds user data. It looks something like this:
ID Name Surname Age
1 Alice Moore 23
2 David Moore 45
If working with only one field, lets say Name, which I have to use for my search key then I could simply do something like this:
Select * from Users where Name Like '% Al %'
ORDER BY DIFFERENCE(Name , 'Al') desc;
and get the data for user 1.
If I want the search operation to take into account other fields too, lets say Surname, I am not sure how to proceed.
How would order by know what column to use in the end so I can return the list based on relevance.
Before anything I'd like to say that I'm not sure how advisable it would be following my solution below in tables with a considerable amount of data. It's simply something I came up that I think should work for your case.
First try
What you could do is order by the minimum difference of all columns of interest in each row.
SELECT FirstName,
LastName,
(SELECT MIN(v)
FROM (VALUES (DIFFERENCE(FirstName , 'Al')),
(DIFFERENCE(LastName , 'Al'))) AS value(v)) as diff
FROM Users WHERE FirstName LIKE 'Al%' OR FirstName LIKE 'Al%'
ORDER BY diff DESC;
Try an interactive fiddle here.
Second try
Another option is ordering your result in a descending order of the difference of the concatenated string of all the rows of interest.
SELECT FirstName,
LastName
FROM Users
WHERE CONCAT (FirstName, LastName) LIKE 'Al%'
ORDER BY
DIFFERENCE(CONCAT (FirstName, LastName) , 'Al') DESC;
Try an interactive fiddle here.

How do I search two columns?

I have a table with First Name, Last Name and Contact Number.
If user enters kar, then I want a full list with results containing kar in the First name or Last Name.
This assumes that kar is a portion of a name, if it is the full name, then do first_name = 'kar'
SELECT first_name, last_name, number
FROM your_phone_book
WHERE first_name LIKE '%kar%'
OR last_name LIKE '%kar%'
It's really rather simple:
SELECT * FROM directory
WHERE firstname LIKE '%kar%'
OR lastname LIKE '%kar%';
select firstname from table
where firstname like '%name%'

how can i write a sql query that finds rows where one column is a substring of another column

I wish to find all rows in a table where one column is a substring of another column.
In other words, suppose I have a table (called people) with two columns: firstname and lastname, and I want to find all people like "rob robinowitz" and "jill bajillion".
Is there a way to do something like "select * from people where lastname like %firstname%"? (But something which actually works).
You were close
select * from people where lastname like '%' + firstname + '%'
Alternative way (may be even faster)
select * from people where charindex(firstname,lastname)>0
If you are using MySQL you could
SELECT * FROM people WHERE INSTR(lastname, firstname) <> 0