Query to display student_no, prj_name and prj_dur - sql

I have following table
TBL_STUDENT (STUDENT_NO VARCHAR2(3) PRIMARY KEY STUDENT_NAME VARCHAR2 (10) STUDENT_DOB DATE STUDENT_DOJ DATE)
TBL_PROJECT (PRJ_NO VARCHAR2 (3) PRIMARY KEY PRJ_NAME VARCHAR2 (15) PRJ_DUR NUMBER (2) PRJ_PLATFORM VARCHAR2 (10))
TBL_STUDENTPROJECT (STUDENT_NO VARCHAR2 (3) PRJ_NO VARCHAR2 (3) DESIGNATION VARCHAR2 (10) PRIMARYKEY (STUDENT_NO,PRJ_NO,DESIGNATION) FOREIGN KEY(STUDENT_NO) of TBL_STUDENT FOREIGN KEY(PRJ_NO) of TBL_PROJECT)
I want to display student_no, prj_name and prj_dur;
select p.prj_name, s.student_no, p.prj_dur from tbl_project p inner join on tbl_studentproject s where p.prj_no = s.prj_no

You need to do Inner join to find prj_name, student_no and prj_dur.
SELECT p.PRJ_NAME, s.STUDENT_NO, p.PRJ_DUR
from TBL_STUDENTPROJECT sp
INNER JOIN TBL_STUDENT s on sp.STUDENT_NO = s.STUDENT_NO
INNER JOIN TBL_PROJECT p ON sp.PRJ_NO = p.PRJ_NO;

If you need to have all the project and project duration of each student then use left join.
SELECT p.PRJ_NAME, s.STUDENT_NO, p.PRJ_DUR
FROM TBL_STUDENT s
LEFT JOIN TBL_STUDENTPROJECT sp on sp.STUDENT_NO=s.STUDENT_NO
LEFT JOIN TBL_PROJECT p ON sp.PRJ_NO=p.PRJ_NO;

Related

Sqlite SQL Statement Not Working As I Expecting

Im using sqlite3 database and i try to get data from two table with cross join.
They have some foreign keys and i can`t get reference value
for currency names (b_currency and s_currency).
They need to be like 'USD','EUR','TRL' etc.
SQL STATEMENT:
select
a.pid,
person.fullname,
a.amount as b_amount,
b.amount as s_amount,
a.currency as b_currency,
b.currency as s_currency,
a.rate as b_rate,
b.rate as s_rate,
`user`.username,
a.`date`
from buy_process as a
inner join person
on a.fullname=person.id
inner join currency
on b_currency=currency.id and s_currency=currency.id
inner join `user`
on a.`user`=`user`.id
cross join sell_process as b
where a.pid=b.pid;
BUY_PROCESS AND SELL_PROCESS TABLE FIELDS ARE SAME:
-- Describe BUY_PROCESS
CREATE TABLE `buy_process`(
id integer primary key AUTOINCREMENT,
pid VARCHAR(50) NOT NULL UNIQUE,
fullname INTEGER NOT NULL,
amount VARCHAR(50) NOT NULL,
currency INTEGER NOT NULL,
rate VARCHAR(50) NOT NULL,
`user` INTEGER NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fkbuy_fullname FOREIGN KEY(fullname) REFERENCES person(id),
CONSTRAINT fkbuy_currency FOREIGN KEY(currency) REFERENCES currency(id),
CONSTRAINT fkbuy_user FOREIGN KEY(`user`) REFERENCES `user`(id)
);
RESULT:
Result image
I tried to change field names but i did not succeed:
a.pid,
person.fullname,
a.amount as b_amount,
b.amount,
currency.name as b_currency,
currency.name as s_currency,
a.rate as b_rate,
b.rate as s_rate,
`user`.username,
a.`date`
from buy_process as a
inner join person
on a.fullname=person.id
inner join currency
on b_currency=currency.id and s_currency=currency.id
inner join `user`
on a.`user`=`user`.id
cross join sell_process as b
where a.pid=b.pid;
I don't understand what you want to achieve with the cross join (you have a condition a.pid=b.pid, why not just inner join them?).
You need to join the currency table twice, once for the buy currency, and once for the sell currency:
select
...
b_cncy.name as b_currency,
s_cncy.name as s_currency,
...
from
buy_process as bp
inner join
sell_process as sp
on bp.pid=sp.pid
inner join
currency b_cncy
on b_cncy.id=bp.currency
inner join currency s_cncy
on s_cncy.id=sp.currency
inner join `user` usr
on usr.id=bp.`user`

Using inner join as ALIASES with multiple tables

I have these tables below:
create table student(
studentName varchar (40) not null,
studentRollNo varchar (30) primary key, -- also acts as username
studentPassword varchar(30) NOT NULL,
studentGender varchar(7) default NULL
);
create table supervisors(
supervisorID varchar(30) foreign key references Faculty(facultyID) unique,
sWorkLoad int default null,
CHECK (sWorkLoad<=6 and sWorkLoad>=0)
);
create table co_supervisors(
co_supervisorID varchar(30) foreign key references Faculty(facultyID) unique,
csWorkLoad int default null,
CHECK (csWorkLoad<=6 and csWorkLoad>=0)
);
create table studentGroup(
groupID int IDENTITY(1,1) primary key ,
Member1rollNo varchar(30) foreign key references student(studentRollNo) default NULL, -- member 1
Member2rollNo varchar(30) foreign key references student(studentRollNo) default NULL, -- member 2
Member3rollNo varchar(30) foreign key references student(studentRollNo) default NULL, -- member 3
supervID varchar(30) foreign key references supervisors(supervisorID),
co_supervID varchar(30) foreign key references co_supervisors(co_supervisorID) default NULL,
projectTitle varchar(100) not null,
projectDetails varchar (500) default NULL
);
create table FYP1(
groupID int foreign key references studentGroup(groupID),
);
I want to display student details who are registered in FYP1.
with their supervisors, co_supervisors,and project title.
But I I'm not able to do so,
What I have done so far is this.
select sg.Member1rollNo,S.studentName,Member2rollNo,S.studentName,sg.Member3rollNo,sg.supervID,sg.projectTitle
FROM student S
inner join studentGroup SG ON S.studentRollNo = SG.Member1rollNo
OR some random tries like this
-- Faculty.facultyName
SELECT FYP1.groupID, studentGroup.Member1rollNo,student.studentName as student1, studentGroup.Member2rollNo,student.studentName as student2,studentGroup.projectTitle
FROM FYP1 as FYP1_Students
INNER JOIN studentGroup ON (studentGroup.groupID = FYP1_Students.groupID)
INNER JOIN supervisors ON (studentGroup.supervID = supervisors.supervisorID)
INNER JOIN student ON (student1.studentRollNo = studentGroup.Member1rollNo)
INNER JOIN student ON (student.studentRollNo = studentGroup.Member2rollNo)
Output or first query is this (example):
'i19-0434' 'Sourav Malani' 'i19-0498' 'Sourav Malani' NULL 'urooj.ghani' 'Indoor Navigation'
'i19-0466' 'Aftab Ali' 'i19-0528' 'Aftab Ali' NULL 'urooj.ghani' 'AI based Physics exp.'
I want output to be like:
'i19-0434' 'Sourav Malani' 'i19-0498' 'Student2 Name' NULL 'urooj.ghani' 'Indoor Navigation'
'i19-0466' 'Aftab Ali' 'i19-0528' '<student2Name>' NULL 'urooj.ghani' 'AI based Physics exp.'
Sample Data
Thanks to #Hana, I solved the problem.
Here's the solution:
SELECT SG.groupID,
SG.Member1rollNo,S1.studentName as 'student1 Name',
SG.Member2rollNo, S2.studentName as 'student2 Name',
SG.Member3rollNo, S3.studentName as 'Studen3 Name',
SG.supervID, SN.facultyName as 'Supervisor Name',
SG.co_supervID, CSN.facultyName as 'Co_Supervisor',
SG.projectTitle as 'Project Title',
SG.projectDetails as 'Project Desc.'
FROM FYP1 FYP1
LEFT OUTER JOIN studentGroup SG ON FYP1.groupID = SG.groupID
LEFT OUTER JOIN supervisors SV ON SG.supervID = SV.supervisorID
LEFT OUTER JOIN Faculty SN ON SG.supervID= SN.facultyID
LEFT OUTER JOIN Faculty CSN ON SG.co_supervID= CSN.facultyID
LEFT OUTER JOIN student S1 ON SG.Member1rollNo = S1.studentRollNo
LEFT OUTER JOIN student S2 ON SG.Member2rollNo = S2.studentRollNo
LEFT OUTER JOIN student S3 ON SG.Member3rollNo = S3.studentRollNo
Is this what you are looking for?
SELECT SG.groupID, SV.supervisorID, CSV.co_supervisorID, S1.studentName, S2.studentName, S3.studentName
FROM FYP1 FYP1
INNER JOIN studentGroup SG ON FYP1.groupID = SG.groupID
INNER JOIN supervisors SV ON SG.supervID = SV.supervisorID
INNER JOIN co_supervisors CSV ON SG.co_supervID = CSV.co_supervisorID
INNER JOIN student S1 ON SG.Member1rollNo = S1.studentRollNo
INNER JOIN student S2 ON SG.Member1rollNo = S2.studentRollNo
INNER JOIN student S3 ON SG.Member1rollNo = S3.studentRollNo

Get user from table based on id

I have these Postgres tables:
create table deals_new
(
id bigserial primary key,
slip_id text,
deal_type integer,
timestamp timestamp,
employee_id bigint
constraint employee_id_fk
references common.employees
);
create table twap
(
id bigserial primary key,
deal_id varchar not null,
employee_id bigint
constraint fk_twap__employee_id
references common.employees,
status integer
);
create table employees
(
id bigint primary key,
account_id integer,
first_name varchar(150),
last_name varchar(150)
);
New table to query:
create table accounts
(
id bigint primary key,
account_name varchar(150) not null
);
I use this SQL query:
select d.*, t.id as twap_id
from common.deals_new d
left outer join common.twap t on
t.deal_id = d.slip_id and
d.timestamp between '11-11-2021' AND '11-11-2021' and
d.deal_type in (1, 2) and
d.quote_id is null
where d.employee_id is not null
order by d.timestamp desc, d.id
offset 10
limit 10;
How I can extend this SQL query to search also in table employees by account_id and map the result in table accounts by id? I would like to print also accounts. account_name based on employees .account_id.
You need two joins to to make this work for you. One join to get to the employee table, and one more join to get to the accounts table.
select d.*, t.id as twap_id, a.account_name
from common.deals_new d
left outer join common.twap t on
t.deal_id = d.slip_id and
d.timestamp between '11-11-2021' AND '11-11-2021' and
d.deal_type in (1, 2) and
d.quote_id is null
join employees as e on d.employee_id = e.id
join accounts as a on a.id = e.account_id
where d.employee_id is not null
order by d.timestamp desc, d.id
offset 10
limit 10;
Note: I did not fiddle this one, so could have a typo, but I think you get the idea here.

I need to create a view that pre-joins the three tables, including all of the records from student and course tables ( shown below) [duplicate]

This question already has answers here:
Error report - ORA-25155: column used in NATURAL join cannot have qualifier 25155. 00000 - "column used in NATURAL join cannot have qualifier"
(2 answers)
Closed 2 years ago.
-- I am trying to create a view for the tables shown below but my attempt is not successfull. I am using Oracle SQL Developer!! Where is the mistake here
CREATE VIEW student_view AS
SELECT Student.*, Course.*, Grade.* FROM (Student NATURAL LEFT OUTER JOIN Grade NATURAL LEFT OUTER JOIN Course)
UNION ALL
SELECT Student.*, Course.*, Grade.* FROM (Course NATURAL LEFT OUTER JOIN Grade NATURAL LEFT OUTER JOIN Student) WHERE Student.StudentID is NULL
;
CREATE TABLE Student(
StudentID INT PRIMARY KEY NOT NULL,
Name CHAR(50),
Address CHAR(50),
GradYear INT
);
-- create table Grade
CREATE TABLE Grade(
CName CHAR(50) NOT NULL,
StudentID INT NOT NULL,
CGrade CHAR(2),
PRIMARY KEY(CName, StudentID)
);
-- create table Course
CREATE TABLE Course(
CName CHAR(50) PRIMARY KEY NOT NULL,
Department CHAR(50),
Credits INT
);
You should create the underlying tables before composing them into a view.
Please follow below sequence,
CREATE TABLE Student(
StudentID INT PRIMARY KEY NOT NULL,
Name CHAR(50),
Address CHAR(50),
GradYear INT
);
-- create table Grade
CREATE TABLE Grade(
CName CHAR(50) NOT NULL,
StudentID INT NOT NULL,
CGrade CHAR(2),
PRIMARY KEY(CName, StudentID)
);
-- create table Course
CREATE TABLE Course(
CName CHAR(50) PRIMARY KEY NOT NULL,
Department CHAR(50),
Credits INT
);
CREATE VIEW student_view AS
SELECT Student.StudentID , Student.Name, Student.Address, Student.GradYear,
Course.CName, Course.CGrade, Grade.Department, Grade.Credits FROM Student
LEFT OUTER JOIN Grade
on (Student.StudentID = Grade.StudentID)
LEFT OUTER JOIN Course
on (Grade.CName = Course.CName);
Corrected version:
Defined tables in the right order
Changed CHAR to VARCHAR2
Added foreign key constraints (inheriting data types)
Removed table aliases not allowed by NATURAL JOIN syntax
Removed redundant brackets from view.
Tables:
create table student
( studentid integer primary key not null
, name varchar2(50) not null
, address varchar2(50)
, gradyear integer );
create table course
( cname varchar2(50) primary key not null
, department varchar2(50)
, credits integer );
create table grade
( cname references course not null
, studentid references student not null
, cgrade varchar2(2) not null
, primary key(cname, studentid) );
View:
create or replace view student_view as
select studentid, name, address, gradyear
, cname, department, credits
, cgrade
from student
natural left outer join grade
natural left outer join course
union all
select studentid, name, address, gradyear
, cname, department, credits
, cgrade
from course
natural left outer join grade
natural left outer join student
where studentid is null;
And just to add, NATURAL JOIN is never a good idea in real code.

Conditional JOIN between tables?

I have two table and I should write a select query which join this two table but I do not know what is conditional join between this two tables?
Can some body say what is?
TABLE ParameterRegistration
(
RegistrationTime DATETIME,
PatiNo VARCHAR(12),
Source VARCHAR(64),
Code VARCHAR(64),
NameOfCodingSystem VARCHAR(64) NULL,
Name VARCHAR(64),
ValueType CHAR(2) NULL,
NumericValue INT NULL,
StringValue VARCHAR(64) NULL,
TextValue TEXT NULL,
Unit VARCHAR(64) NULL,
UnitCode VARCHAR(64) NULL,
UnitCodingSystem VARCHAR(64) NULL,
Remark VARCHAR(255) NULL,
CreateDate DATETIME,
CreateUserId T_USER_ID
)
and
TABLE External
(
ModDate DATETIME,
ModUserId VARCHAR(12),
UbMem VARCHAR(64),
Code VARCHAR(64),
Name VARCHAR(64),
Service VARCHAR(64),
NameOfCodingSystem VARCHAR(64) NULL,
)
You can only properly join those two tables if they have a relation of some kind.
Check THIS ARTICLE for some info.
Assuming that in your case, the tables are related with the columns Code, NameOfCodingSystem and Name you can make a join like this:
select p.*, e.* from ParameterRegistration p
inner join External e on p.Code = e.Code and
p.NameOfCodingSystem = e.NameOfCodingSystem and
p.Name = e.Name
When joining tables you have to join on fields that are related. Since you did not provide a lot of information it looks like you have 3 fields that possibly could be joined on.
NameOfCodingSystem VARCHAR(64)
Name VARCHAR(64)
Code VARCHAR(64)
So you could technically write your query one of these ways:
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.NameOfCodingSystem = E.NameOfCodingSystem
OR
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.Name = E.Name
OR
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.Code = E.Code
OR you can join on all of the fields at the same time
SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.NameOfCodingSystem = E.NameOfCodingSystem AND
P.Name = E.Name AND
P.Code = E.Code
My suggestion would be to study up on JOINs. Here are some resources but there are plenty on the internet:
A Visual Explanation of Joins
SQL Joins
Joins