SQL QUERY disturbing Error - sql

I have to write a query which displays the First and Last Name for all users who belong to the Role “Lumo Advantage”.
I wrote it as:
SELECT Users.FirstName, Users.LastName
INNER JOIN Users
ON UserRoles.UserID = Users.UserID
WHERE UserRoles.RoleID =7;
But it is showing error. why? Kindly point out the mistake.

Perhaps because you left out a from clause:
SELECT Users.FirstName, Users.LastName
FROM Users INNER JOIN
UserRoles
ON UserRoles.UserID = Users.UserID
WHERE UserRoles.RoleID = 7;
I would suggest you learn to use table aliases . . . so queries are easier to write and to read:
SELECT u.FirstName, u.LastName
FROM Users u INNER JOIN
UserRoles ur
ON ur.UserID = u.UserID
WHERE ur.RoleID = 7;

Related

SQL Group By and Having clause and exists clause

These queries fetch records from multiple tables(AspNetUsers,AspNetUserRoles & AspNetRoles). The records will include only those users which have multiple Roles.
I am looking for reasons why 1st query works and the latter did not. Any help would be appreciated.
Query 1:
SELECT
U.Id,
U.UserName
,R.Id
,R.Name AS RoleName
FROM AspNetUsers AS U
JOIN AspNetUserRoles UR
ON U.Id = UR.UserId
JOIN AspNetRoles AS R
ON R.Id = UR.RoleId
WHERE EXISTS (
SELECT UserId,
COUNT() AS NumberofRoles
FROM AspNetUserRoles
GROUP BY UserId
HAVING COUNT() > 1)
Query 2:(Only work if I remove R.Id & R.Name Otherwise it is not working)
SELECT
U.Id,
U.UserName
,R.Id
,R.Name AS RoleName
FROM AspNetUsers AS U
JOIN AspNetUserRoles UR
ON U.Id = UR.UserId
JOIN AspNetRoles AS R
ON R.Id = UR.RoleId
GROUP BY U.Id,U.UserName
The table diagram is attached for better clarity.
The second query doesn't woek because you haven't defined anything known as R.
try this :
SELECT
U.Id,
U.UserName
,R.Id
,R.Name AS RoleName
FROM AspNetUsers AS U
JOIN AspNetUserRoles UR
ON U.Id = UR.UserId
JOIN AspNetRoles AS R
ON R.Id = UR.RoleId
GROUP BY U.Id,U.UserName,R.Id,R.Name
This would be the correct way to fetch records that have multiple Roles.
SELECT
U.Id,
U.UserName
,R.Id AS RoleID
,R.Name AS RoleName
FROM AspNetUsers AS U
JOIN AspNetUserRoles UR
ON U.Id = UR.UserId
JOIN AspNetRoles AS R
ON R.Id = UR.RoleId
WHERE U.Id IN (SELECT UserId FROM AspNetUserRoles GROUP BY UserId HAVING COUNT(*) > 1)

SQL statement, Joins

select userId, firstname, lastname, userVehicleId
from users
I want to make an inner join between users and userVehicle. This is what I have so far, can anyone help out?
Inner Join
select userId, firstname, lastname, userVehicleId
from users
inner join userVehicleId
on userVehicle
where users.userId = userVehicle.userVehicleId
Your question is not clear, may be you need this query?
select u.userId, u.firstname, u.lastname, u.userVehicleId
from users u
inner join userVehicle uv
on uv.id=u.id
As far as I understand, you want to get userId, firstname and lastname for users who have vehicles, right?
It can be something like this:
SELECT u.userId, u.firstname, u.lastname, uv.userVehicleId
FROM users u
INNER JOIN userVehicle uv
ON u.userId=uv.userVehicleId
Please, provide structure for users and userVehicle tables to get more accurate answer.

SELECT all from left, only from right only where

I have 3 tables, Roles, UsersInRoles and Users
I want to select all roles and a only user records where the user matches the where clause
SELECT r.*, u.UserName
FROM [dbo].[Roles] r
LEFT JOIN [dbo].[UsersInRoles ] ur
ON r.Id = ur.RoleId
LEFT JOIN [dbo].[Users] u
ON u.Id = ur.UserId
WHERE u.UserName = 'admin'
What I want is this:
RoleId, Role, Username
1 Admin Admin
2 Student Null
When I include the where clause, the student role is not returned
Move the filter from your WHERE clause, to the join condition of your Users table:
SELECT r.*, u.UserName
FROM [dbo].[Roles] r
LEFT JOIN [dbo].[UsersInRoles ] ur
ON r.Id = ur.RoleId
LEFT JOIN [dbo].[Users] u
ON u.Id = ur.UserId
AND u.UserName = 'admin'
This will return all records from Roles, with any matching records from UsersInRoles, but only those matching records in Users whose UserName equals 'admin'.
SELECT r.*, u.UserName
FROM [dbo].[Roles] r
LEFT JOIN [dbo].[UsersInRoles ] ur
ON r.Id = ur.RoleId
LEFT JOIN [dbo].[Users] u
ON u.Id = ur.UserId
WHERE (u.UserName = 'admin' or u.UserName is null)

SQL Get List of Users not in Roles

I have three tables:
Users : UserID, UserName
Roles : RoleID, RoleName
UsersInRoles : UserID, RoleID
How do I get a list of UserIDs and the RolesIDs for which they are NOT in?
I'm using SQL Server 2012.
Thanks for any help you can provide.
Aaron
select users.userid, roles.roleid
from users
cross join roles
left outer join usersinroles on
usersinroles.userid = users.userid and
usersinroles.roleid = roles.roleid
where usersinroles.userid is null
cross join joins each role to each user.
left outer join joins the tables, but doesn't delete the rows that don't match. Instead, it leaves the joined fields as null when there is no match. Getting only the cases where the field is null has the effect of getting only the rows that do not match--the roles that a user doesn't have.
I think #dan1111's solution is better, but this one might be more readable:
SELECT u.Userame,
r.RoleName
FROM Users u
CROSS JOIN Roles r
EXCEPT
SELECT u.Userame,
r.RoleName
FROM Users u
INNER JOIN UsersInRoles ur
on u.UserID = ur.UserID
INNER JOIN Roles r
on ur.RoleID = r.RoleID
Consider using a LEFT JOIN and combine it with an IS NULL condition.
This works using a CROSS JOIN and a LEFT JOIN:
SELECT U.UserId, R.RoleId
FROM Roles R CROSS JOIN Users U
LEFT JOIN UserRoles UR ON U.UserId = UR.UserId AND R.RoleId = UR.RoleId
WHERE UR.UserId IS NULL
And here is the sample Fiddle: http://sqlfiddle.com/#!6/46e48/1

Help with MySQL Query?

I have two tables rooms and users. I want to get only rooms.room_id, users.user_name with user_id = 1. I can get the result of all users with following sql...
select rooms.room_id,
rooms.user_id,
users.user_name
from rooms
LEFT JOIN users ON rooms.user_id = users.user_id
When I do like this to filter the result with user_id = 1 ... I got error.
select rooms.room_id,
rooms.user_id,
users.user_name
from rooms where rooms.user_id = 1
LEFT JOIN users ON rooms.user_id = users.user_id
What should I do?
ANSI-92 JOIN syntax (when you see LEFT JOIN ...) dictates that the WHERE clause comes after the JOIN(s):
SELECT r.room_id,
r.user_id,
u.user_name
FROM ROOMS r
LEFT JOIN users ON u.user_id = r.user_id
WHERE r.user_id = 1
You were close.
Write the query as:
select rooms.room_id,
rooms.user_id,
users.user_name
from rooms
LEFT JOIN users ON rooms.user_id = users.user_id
WHERE roows.user_id = 1
Try:
select rooms.room_id,
rooms.user_id,
users.user_name
from rooms
LEFT JOIN users ON rooms.user_id = users.user_id
WHERE rooms.user_id = 1
The syntax of a simple SQL SELECT query is:
SELECT [a list of fields]
FROM [a single table name maybe with an alias, or a join of tables]
WHERE [a filter, applied over some fields of the tables in the FROM clause]
You could read an introductory tutorial here.
It helps to state what error you get.
I would guess that the problem is that the where clause needs to be after the joins
select rooms.room_id,
rooms.user_id,
users.user_name
from rooms
LEFT JOIN users ON rooms.user_id = users.user_id
where rooms.user_id = 1