SQL statement, Joins - sql

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.

Related

SQL QUERY disturbing Error

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;

Select all values from first table and ONLY first value from second table

I want to select all values from Users and min of dateUsed in Code table. How can I do that?
I've tried this:
SELECT u.firstName, u.lastName, u.fbId, q.dateUsed, u.codesLeft
FROM Users u
inner join Code q on u.Id = q.userId
But it's selecting all values from the Code and Users tables.
P.S. Adding distinct has no effect
Adding distinct has no effect
As a rule of thumb, DISTINCT helps for a single-column SELECTs. With multiple columns you need to go for "big guns" - the GROUP BY clause.
In order for that to work you need to make sure that each item that you select is either a GROUP BY column, or has a suitable aggregation function:
SELECT u.firstName, u.lastName, u.fbId, MIN(q.dateUsed) as dateUsed, u.codesLeft
FROM Users u
INNER JOIN Code q ON u.Id = q.userId
GROUP BY u.firstName, u.lastName, u.fbId, u.codesLeft
Scalar Sub Query
SELECT u.firstName,
u.lastName,
u.fbId,
(SELECT TOP 1 dateUsed
FROM Code as q WHERE u.Id = q.userId
ORDER BY dateUsed ASC),
u.codesLeft
FROM Users u
OR CTE
;WITH SEQ as (
SELECT userId, MIN(DateUsed) as FirstDate, MAX(DateUsed) as lastDate
FROM Code GROUP BY userID)
SELECT... u.*, q.FirstDate, q.LastDate
FROM Users as u
JOIN SEQ as q ON u.ID = q.userID
If you want to see more from the Code table than just the date, you can use a correlated subquery in the join:
SELECT u.firstName, u.lastName, u.fbId, q.dateUsed, u.codesLeft,
q.codeVal, q.ipUsed
FROM Users u
join Code q
on q.userId = u.Id
and q.dateUsed =(
select Min( dateUsed )
from Code
where userID = q.userID );

retrieving values from 3 one-to-one relationship table (Oracle)

Each Student and Teacher have their own UNIQUE UserID
Is it possible to retrieve the values from the three tables at once?
such that: it will display the UserID owned by each Student or Teacher?
I've tried the following query, but it doesn't work:
SELECT u.UserID, StudentID, TeacherID
FROM User u
INNER JOIN (SELECT * FROM Student, Teacher) ss
ON u.UserID = ss.UserID
Maybe this way:
SELECT u.UserID, S.StudentID, T.TeacherID
from User U
left join Student S on S.UserID = u.UserID
left join Teacher T on T.UserID = U.UserID

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

possible to join a table on only one row

I have a temporary table I'm creating in a sproc that houses my user information. I need to join this table to another table that has SEVERAL rows for that particular user but I only want to return one result from the "many" table.
something like this
SELECT u.firstname, u.lastname
FROM #users AS u
INNER JOIN OtherTable AS ot on u.userid = (top 1 ot.userid)
obviously that wont' work but that's the gist of what I'm trying to do for two reasons, one I only want one row returned (by a date field descending) and two for optimaztion purposes. The query has to scan several thousand rows as it currently is..
SELECT
u.firstname, u.lastname, t.*
FROM
#users AS u
CROSS APPLY
(SELECT TOP 1 *
FROM OtherTable AS ot
WHERE u.userid = ot.userid
ORDER BY something) t
Use the ROW_NUMBER() function to order your rows by datetime and then filter by row_num = 1
;WITH otNewest
AS
(
SELECT *
FROM othertable
WHERE ROW_NUM() OVER(partition by userid order by datetime DESC) = 1
)
SELECT u.firstname, u.lastname, o.*
FROM #users U
INNER JOIN otNewest O
ON U.userid = O.userid
So, if you're joining but not returning any columns from OtherTable, then you're only interested in checking for existence?
SELECT u.firstname, u.lastname
FROM #users AS u
WHERE EXISTS(SELECT 1
FROM OtherTable ot
WHERE u.userid = ot.userid)