SELECT, FROM, INNER JOIN, WHERE function - sql

I have two tables "users" and "emp_details". "users" are Branch mangers who have login id for monitoring their own branch's employees. "emp_details" includes employees working for different branches which include branch managers aslo.
when Branch manager ("users") log in it should fetch details from "emp_details" only if this particular "users" branch (code: location) match.
SELECT emp_details.emp_no, emp_name, designation, emp_details.location
FROM emp_details
INNER JOIN users
WHERE users.location = emp_details.location
used above code but showing data for all users created not as per the login-ed users location. Please help

From your query it will give the output of all the data which have matching locations.
If you want to get the data which belong to the location of the specific user, you have to filter that user from the query. Otherwise query will output all the matching data.
DECLARE #UserName AS Varchar(50)
SELECT ed.emp_no, ed.emp_name, ed.designation,ed.location
FROM emp_details ed
INNER JOIN users u ON u= ed.location
WHERE u.User_Name = #UserName
Please note that above parameter can be user name or user ID or any relevant thing, but you have to pass that to the sql query.

It looks like you want a WHERE clause -- after fixing your WHERE clause to be an ON clause:
SELECT ed.emp_no, ed.emp_name, ed.designation, ed.location
FROM emp_details ed JOIn
users u
ON u.location = ed.location
WHERE u.login_id = ?;
? is a parameter placeholder for the manager's login id. That seems to be how you are identifying the users.

Related

How to select records from database table which has to user id (created_by_user, given_to_user) and replace users id by usernames?

This is task table:
This is user table:
I want to select user tasks.
I would give from backend ("given_to_user) id.
But The thing is I want that SELECTED data would have usernames instead of Id which is (created_by_user and given_to_user).
SELECTED table would look like this.
Example:
How to achieve what I want?
Or maybe I designed poorly my tables that It is difficult to select data I need? :)
task table has to id values that are foreign keys to user table.
I tried many thinks but couldn't get desired result.
You did not design poorly the tables.
In fact this is common practice to store the ids that reference columns in other tables. You just need to learn to implement joins:
SELECT
task.id, task.title, task.information, user.usename AS created_by, user2.usename AS given_to
FROM
(task INNER JOIN user ON task.created_by_user = user.id)
INNER JOIN user AS user2 ON task.created_by_user = user2.id;
Do you just want two joins?
select t.*, uc.username as created_by_username,
ug.username as given_to_username
from task t left join
users uc
on t.created_by_user = uc.id left join
users ug
on t.given_to_user = ug.id;
This uses left join in case one of the user ids is missing.

SQL Column Comparison

I have two queries I need in one table, and haven't been able to find results in searching.
In the first table there is a UserID, which is a number, along with what the actual user id is and user's name.
My issue is that in every other query the user is only referred to as this number. I'm needing help in how to translate this UserID value into the actual User's name in my other queries.
Table with User's ID and User's Name:
SELECT *
FROM [Table].[dbo].[User]
Table example where the user is only referred to as this number, along with all other tables:
SELECT *
FROM [Table].[LoginStatus]
You need to join the two tables:
SELECT u.[UserName], l.*
FROM [LoginStatus] l
JOIN [Users] u ON u.id = l.user_id

How do I get all the institutions in my SQL query in oracle with inner join?

I'm trying to write a query in oracle SQL, first I am trying to edit a profile, so I want to change the institution that the user previously select it, but for that I must get all the institutions from a table named institution (id_institution, name_institution),
select user.user_name, institution.name_institution from user
inner join institution_has_user on user.id_user = institution_has_user.user_id_user
inner join institution on institution_has_user.institution_id_institution = institution.id_institution
where user = 'george';
But all I get It is the data that he register, and I want all the institutions, so I can fill a select html, just for editing purposes
If you want to return all the institutions from your above query, then you'll need to use an OUTER JOIN and move your WHERE criteria to your JOIN. This will however return NULL records for the user name field where there aren't any matches which might not be what you're looking for:
select distinct user.user_name, institution.name_institution
from institution
left join institution_has_user on institution_has_user.institution_id_institution = institution.id_institution
left join user on user.id_user = institution_has_user.user_id_user
and user.user_name = 'george';
Perhaps an easier alternative solution, just run two queries -- it should be perfectly fine to create a drop down list from a single select statement:
select name_institution
from institution

Issues with subqueries for stored procedure

The query I am trying to perform is
With getusers As
(Select userID from userprofspecinst_v where institutionID IN
(select institutionID, professionID from userprofspecinst_v where userID=#UserID)
and professionID IN
(select institutionID, professionID from userprofspecinst_v where userID=#UserID))
select username from user where userID IN (select userID from getusers)
Here's what I'm trying to do. Given a userID and a view which contains the userID and the ID of their institution and profession, I want to get the list of other userID's who also have the same institutionID and and professionID. Then with that list of userIDs I want to get the usernames that correspond to each userID from another table (user). The error I am getting when I try to create the procedure is, "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.". Am I taking the correct approach to how I should build this query?
The following query should do what you want to do:
SELECT u.username
FROM user AS u
INNER JOIN userprofspecinst_v AS up ON u.userID = up.userID
INNER JOIN (SELECT institutionID, professionID FROM userprofspecinst_v
WHERE userID = #userID) AS ProInsts
ON (up.institutionID = ProInsts.institutionID
AND up.professionID = ProInsts.professionID)
Effectively the crucial part is the last INNER JOIN statement - this creates a table constituting the insitutionsids and professsionids the user id belongs to. We then get all matching items in the view with the same institution id and profession id (the ON condition) and then link these back to the user table on the corresponding userids (the first JOIN).
You can either run this for each user id you are interested in, or JOIN onto the result of a query (your getusers) (it depends on what database engine you are running).
If you aren't familiar with JOIN's, Jeff Atwood's introductory post is a good starting place.
The JOIN statement effectively allows you to explot the logical links between your tables - the userId, institutionID and professionID are all examples of candidates for foreign keys - so, rather than having to constantly subquery each table and piece the results together, you can link all the tables together and filter down to the rows you want. It's usually a cleaner, more maintainable approach (although that is opinion).

Stored procedure to return multiple records based on multiple conditions

I am new to SQL server, thus looking for some quick help on writing stored procedure:
brief about what I am doing:
employee says he is expert (type) in different domains (industries) and willing to work in countries of choice (mycountries) and my sal (minsal)and my native country (orgcountry)
Employer says he need so and so expert in the his choice of domain (industries) in the countries where openings are there and with sal range.
employee table has lots of records with columns like this:
name, email, myindustries, mycountries, mytype,minsal
employer table has lots of records with columns like:
expertneed, inindustries, incountries, sal-from, sal-to
now when employee logs in, he/she should get all the records of matching employers
when employer logs in, he/she also get all the records of matching employees.
can some one help in writing sp for this? appreciate any help
If you're storing comma-separated ids then you'll need a function to split a string into multiple rows. This is how you would use it:
SELECT DISTINCT employee.name [employee], employer.name [employer]
FROM employee
OUTER APPLY dbo.split(employee.myindustries) myi (industry)
OUTER APPLY dbo.split(employee.mycountries) myc (country)
JOIN employer
OUTER APPLY dbo.split(employer.inindustries) ini (industry)
OUTER APPLY dbo.split(employer.incountries) inc (country)
WHERE employer.expertneed = employee.type
AND ini.inindustries = myi.industry
AND inc.incountries = myc.country
AND employee.minsal BETWEEN employer.[sal-from] AND employer.[sal-to]