Main reason of using "^" - sql

I don't understand what what the purpose of using "^" in the second code?
Another problem is that I don't retrieve right information in second code compare to the first code. Why?
-- First code, use adventurework DW 2008
select FirstName, LastName from DimCustomer
where LastName like '[j-N]%'
order by LastName
-- Second code, use adventurework DW 2008
select FirstName, LastName from DimCustomer
where LastName like '[^L-N]%'
order by LastName

^ means where the character is not within the specified range.
Which will probably explain why you don't get the results you though you would?

Related

Ambiguous column name - whats wrong with the syntax here?

I cant seem to figure out what is wrong with the SQL here, when an asterisk (*) is included in the SELECT statement. eg:
SELECT firstname, lastname, createdon, * from Person
Where firstname like '%J%'
Order By createdon desc
I get 'Ambiguous column name 'CreatedOn'. error.
However without the (*) where its not returning the remainder of the columns it works fine.
Any suggestion or workaround?
createdon is in the returned dataset twice, once from the explicit call, and once from the *. As such there are 2 columns with the name createdon and so ORDER BY createdon is ambiguous; do you want to order by the first column named createdon or the second one (the fact that they would have same value is irrelevant, as SQL Server just sees 2 columns with the same name).
Really, you shouldn't be using SELECT * and should be defining all your columns; I don't see a need to return 3 of columns twice. But what you can do if you "must" have 2 copies of the same column is to prefix the one in the ORDER BY with the table name/alias; this would then refer to the column "in" the table, not the one in the result set (of which there are 2). I use an alias here:
SELECT P.firstname,
P.lastname,
P.createdon,
* --Replace this with the actual columns you need, don't be lazy
FROM dbo.Person P
WHERE P.firstname like '%J%'
ORDER BY P.createdon DESC;
In your query, you are selecting "createdon" column twice and but ordering the results by "one of them. The SQL interpreter doesn't understand which "createdon" column it needs to sort the result and so Ambiguous column name is returned. You can solve this by using aliasing one of the "createdon" in the selected statement.
im not sure but try with that
SELECT firstname, lastname, createdon createdon2, * from Person
Where firstname like '%J%'
Order By createdon desc

SQL: Using the AND statement with nested queries

I am learning with an online tutorial series and right now we are looking into nested queries. Trying to play around and think of ways I could play with this concept I wanted to try to make combined nested queries, but I am not really sure how and google isn't providing me much luck. Once again it's hard to word things like this. I am using MS SQL
SELECT EmployeeID, FirstName, LastName
FROM SQLTutorial.dbo.EmployeeDemographics
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.EmployeeSalary
WHERE JobTitle = 'DBA')
/*
AND
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.WareHouseEmployeeDemographics
WHERE Age = 29)
*/
This is what I thought I could do. From the example I know that the uncommented part works. It gets the ID, First Name, and Last name from EDemo IF the ID is in BOTH EDemo AND ESalary AND their job title is DBA.
Well I wanted to limit those results further by only having the results be those who work in the Warehouse as well AND are 29.
I mean if I run the commented code without the uncommented it works like intended as well, but I am not sure why I cannot just combine the two. I am 90% sure it's b/c I can't use the AND statement like this and I have a sneaking feeling I have to nest the two together and make a nest in a nest.
And it will be like a taco inside taco within a Taco Bell that's inside a KFC that's within a mall that's inside your dream! Sorry I couldn't help myself.
Just remove second second "Where" and your query is good to execute. You just need one where clause within which you can have all your condition combined with and, or etc..
SELECT EmployeeID, FirstName, LastName
FROM SQLTutorial.dbo.EmployeeDemographics
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.EmployeeSalary
WHERE JobTitle = 'DBA')
AND EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.WareHouseEmployeeDemographics
WHERE Age = 29)
If you want all the employees whose ID is in BOTH EDemo AND ESalary AND their job title is DBA but or who work in the Warehouse AND are 29.
SELECT EmployeeID, FirstName, LastName
FROM SQLTutorial.dbo.EmployeeDemographics
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.EmployeeSalary
WHERE JobTitle = 'DBA')
or EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.WareHouseEmployeeDemographics
WHERE Age = 29)
As pointed out in the comments, the issue is having two WHEREs. Only the first WHERE is needed; after specifying WHERE, conditions can just be joined using AND, OR, that sort of thing. Uncommenting and removing the second WHERE should fix it. So, instead of "WHERE a=b AND WHERE c=d" just write "WHERE a=b AND c=d".

Clarification on database architecture question

I'm working on a quiz for this internship and one question is worded strangely. I'm hoping that one of you could help me find clarification.
Context: I've just created a flat-file table (database), and added 4 columns (UserId,firstname,lastname,email). I filled each column with 15 rows of made-up data.
The question states "Query all rows in the firstname column that start with the letter B and ordered by the lastname column in descending order."
I'm not sure what they're asking for, does this make sense to you?
The question is asking you to query the table, selecting the firstname column and all data that starts with the letter 'B'. And then ordered by the lastname. It's a fairly basic query:
SELECT t.firstname
FROM tablename t
WHERE t.firstname like 'B%'
ORDER BY t.lastname DESC;
EDIT: I did forget the DESC;
The query consists in retrieving first names and last names of rows where the firstname starts with the letter B, and then to order the resulting set using the lastname field in descending order.
The query in sql would be like:
SELECT firstname, lastname FROM users WHERE firstname LIKE 'B%' ORDER BY lastname DESC;

SQL - retrieval query for specific string

I am making a small database at the moment (less than 50 entries) and I am having trouble with a query. My query at the moment is
SELECT Name
FROM Customers
WHERE Name LIKE '%Adam%'
The names are in the format of "Adam West".
The query works fine in retrieving all the people with "Adam" in their name but I would like to only retrieve the first name, not the last name. I don't want to split the columns up but would like to know how to rewrite my query to account for this.
SELECT Name
FROM Customers
WHERE Name LIKE 'Adam%'
if you are storing name with space as separator example "Adam abcd" where 'Adam' is firstname and 'abcd' as lastname then following will work
SELECT Expr1
FROM (SELECT LEFT(Name, CHARINDEX(' ', Name, 1)) AS Expr1
FROM Customers) AS derivedtbl_1
WHERE (Expr1 LIKE 'Adm%')
for more details read this article http://suite101.com/article/sql-functions-leftrightsubstrlengthcharindex-a209089
If the name of your customers starts with first name (like Adam West) you can use
select Name
from Customers
where Name like 'Adam %'
Otherwise, if the format of the name is <last name> <first name> you can use
select Name
from Customers
where Name like '% Adam'
Try this
{SELECT Name
FROM Customers
WHERE SUBSTRING(Name,1,CHARINDEX(' ',Name)-1) LIKE '%Adam%'
}
I assumed the first name and last name is saparated by space and i took the firstname out
Using SUBSTRING(Name,1,CHARINDEX(' ',Name)-1)
and compared how you wanted. if your have something else saparating first name and last name change space in charindex with the saparater.
Please let me know if any other help needed.
Regards
Ashutosh
You should indeed "split" your columns and normalize your tables to avoid having to use complicated string functions to search for a lastname or firstname or what ever you need to look for. What about if someone entered lastname first and then firstname? Or just a nickname?
That said, check the use of LIKE on Microsoft technet site. The following query should be helpful on you case:
SELECT Name
FROM Customers
WHERE Name LIKE 'Adam%'

Different ways to alias a column

What is the difference between
select empName as EmployeeName from employees
versus
select EmployeeName = empName from employees
from a technical point of view. Not sure if this is just SQL server specific or not.
Appreciate your answers.
I'd prefer the first one, since the second one is not portable -
select EmployeeName = empName from employees
is either a syntax error (at least in SQLite and Oracle), or it might not give you what you expect (comparing two columns EmployeeName and empName and returning the comparison result as a boolean/integer), whereas
select empName EmployeeName from employees
is the same as
select empName as EmployeeName from employees
which is my preferred variant.
The main advantage of the second syntax is that it allows the column aliases to be all lined up which can be of benefit for long expressions.
SELECT foo,
bar,
baz = ROW_NUMBER() OVER (PARTITION BY foo ORDER BY bar)
FROM T
I don't think there's a technical difference. Its mainly preferential. I go for the second as its easier to spot columns in big queries, especially if the query is properly indented.