Why do i receive 'Duplicate column name' error when innerjoin-ing 2 tables with similar column name? - sql

CREATE TABLE student_activestudent AS
(
SELECT *
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
I am expecting a table with 2 columns of studentnumber but I received Duplicate error instead --> Duplicate column name 'studentnumber'

A database table must have unique column names.
When you do select * you will get all columns from all tables and studentnumber exists on both student table and activestudent table. So to solve you problem specify the columns you want instead of *
CREATE TABLE student_activestudent AS
(
SELECT
student.studentnumber,
..Other columns..
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);

At the very least, studentnumber is duplicated. In general, I strongly recommend that a view list all the columns explicitly. This protects the view if underlying columns change.
That said, if studentnumber is the only column, then you can do:
CREATE TABLE student_activestudent AS
SELECT *
FROM student s JOIN
activestudent ast
USING (studentnumber);
With using, the * does not repeat the join keys.

You cannot select two tables that have same column's name.
The best way is not to select *
Select by column and if the column is same you can put [as]
Example
SELECT student.studentnumber as stuNumber, activestudent.studentnumber as actstuNumber

Related

trying to match a column with a list of column names to a table that has those columns with their own values SQL

I have a table A that is a singular column called "Column_Name" and its values are a list of names. I have another table B whose columns are those values of "Column_Name" in table A. For example the values of "Column_Name" in table A are (Date, Age, Location). Then in table B, its actual columns are (Date, Age, Location) and those columns have their own respective values. I am trying to select the columns from table B that are values in "Column_Name" in table A. Is there a way to do this? I think I can use the IN operator in a WHERE clause, but not exactly sure how.
I don't know if I got it correctly,
Considering that the column_name in tableA is separated by commas,
would it be something like this?
select * from tableB
where Dt in (select SUBSTRING_INDEX(Column_Name,',',1) from tableA)
or Age in (select SUBSTRING_INDEX(Column_Name,',',2) from tableA)
or Location in (select SUBSTRING_INDEX(Column_Name,',',3) from tableA);
here is the example
https://www.db-fiddle.com/f/pUytYwKpK4mRsk76oFonV8/0

Oracle SQL Subquery - Usage of NOT EXISTS

I used a query to find a list of Primary Keys. One Primary key per each ForiegnKey in a table by using below query.
select foreignKey, min(primaryKey)
from t
group by foreignKey;
Let us say this is the result : 1,4,5
NOw I have another table - Table B that has list of all Primary keys. It has 1,2,3,6,7,8,9
I want a write a query using the above query So that I get a subset of the original query(above) that does not exist in Table B. I want 4 and 5 back with the new query.
Use a having clause:
select foreignKey, min(primaryKey)
from t
group by foreignKey
having min(primarykey) not in (select pk from b);
You should also be able to express this as not exists:
having not exists (select 1
from b
where b.pk = min(t.primaryKey)
)

Why won't this query update the field with the lookup?

I'm trying to run the following query to update one table from another. The dates and email address work and carry across, but the nested query I'm using to get Subject_1 from a reference table does not. What am I doing wrong?
SELECT
FirstRegistered As SignUpdate,
(SELECT Subj_ClusName FROM tblSubjectLookup INNER JOIN PAD_ApplicantLost2000 ON tblSubjectLookup.Subj_Name=PAD_ApplicantLost2000.raw_subj_interest_1) AS Subject_1,
Email_Address
FROM PAD_ApplicantLost2000
The origin table, PAD_ApplicantLost2000, has a 'raw subject' column which contains, for example, 'Biology'. There is another table, tblSubjectLookup, which has codes for all subjects, so Subj.Name has 'Biology' and 'Subj_ClusName' has 'B1', which is what needs to go in my target table. However, the Subject_1 field in the target table does not populate.
What am I doing wrong?
Why not just use a WHERE clause instead of joining the table again. I also added LIMIT 1 just to make sure only 1 value is returned.
SELECT
FirstRegistered As SignUpdate,
(SELECT Subj_ClusName FROM tblSubjectLookup WHERE tblSubjectLookup.Subj_Name = PAD_ApplicantLost2000.raw_subj_interest_1 LIMIT 1) AS Subject_1,
Email_Address
FROM PAD_ApplicantLost2000
Another, possibly better, way to do it would just be to join the table directly.
SELECT
FirstRegistered As SignUpdate,
Subj_ClusName AS Subject_1,
Email_Address
FROM PAD_ApplicantLost2000
LEFT JOIN tblSubjectLookup ON tblSubjectLookup.Subj_Name = PAD_ApplicantLost2000.raw_subj_interest_1

SQL query to select * from table, but return ID column as looked-up usernames

I'm using Bugzilla, and I essentially want to SELECT * FROM bugs table in the "bugs" database. However, the "assigned_to" column actually contains integer values (IDs) instead of a string with the user name.
These IDs match primary keys in the "profiles" table (the "userid" column), and the string I want my query to return is actually stored in the "realname" column in that table.
How can I modify this query to capture all columns in "bugs," but perform a lookup on the assigned_to column and return usernames?
SELECT b.*, p.realname FROM bugs b
JOIN profiles p
ON b.assigned_to = p.userid

Underlying rows in Group By

I have a table with a certain number of columns and a primary key column (suppose OriginalKey). I perform a GROUP BY on a certain sub-set of those columns and store them in a temporary table with primary key (suppose GroupKey). At a later stage, I may need to get more details about one or more of those groupings (which can be found in the temporary table) i.e. I need to know which were the rows from the original table that formed that group. Simply put, I need to know the mappings between GroupKey and OriginalKey. What's the best way to do this? Thanks in advance.
Example:
Table Student(
StudentID INT PRIMARY KEY,
Level INT, --Grade/Class/Level depending on which country you are from)
HomeTown TEXT,
Gender CHAR)
INSERT INTO TempTable SELECT HomeTown, Gender, COUNT(*) AS NumStudents FROM Student GROUP BY HomeTown, Gender
On a later date, I would like to find out details about all towns that have more than 50 male students and know details of every one of them.
How about joining the 2 tables using the GroupKey, which, you say, are the same?
Or how about doing:
select * from OriginalTable where
GroupKey in (select GroupKey from my_temp_table)
You'd need to store the fields you grouped on in your temporary table, so you can join back to the original table. e.g. if you grouped on fieldA, fieldB, and fieldC, you'd need something like:
select original.id
from original
inner join temptable on
temptable.fieldA = original.fieldA and
temptable.fieldB = original.fieldB and
temptable.fieldC = original.fieldC