SQL SELECT FROM TWO TABLES INNER JOIN - sql

I am trying select from my Album and ArtistBand Table and display the Name and Bandname in alphabetical order. When in try to use the select statement below it shows up with all the albums on each band. Any help would be appreciated project for school.
CREATE TABLE ArtistBand
(
ArtistID INT AUTO_INCREMENT PRIMARY KEY,
BandName VARCHAR(255) NOT NULL,
NameOfMembers VARCHAR(255),
NumberOfMembers INT
);
CREATE TABLE Genre
(
GenreID INT AUTO_INCREMENT PRIMARY KEY,
GenreType VARCHAR(255) NOT NULL
);
CREATE TABLE Album
(
AlbumID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR (255) NOT NULL,
ReleaseDate DATE NOT NULL,
Producers VARCHAR (255),
ArtistID INT,
GenreID INT,
FOREIGN KEY(ArtistID) REFERENCES ArtistBand(ArtistID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);
CREATE TABLE LookUp
(
AlbumID INT,
GenreID INT,
FOREIGN KEY (AlbumID) REFERENCES Album (AlbumID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);
INSERT INTO ArtistBand
VALUES
(NULL,'ACDC','BrianJohnson',4),
(NULL,'Metallica','JamesHeitfield',4),
(NULL,'TheBeatles','JohnLennon',4),
(NULL,'JayZ','ShawnCarter',1),
(NULL,'DangerMouse','BrianBurton',1);
INSERT INTO Album
VALUES
(NULL,'BackInBlack','1980-01-01',NULL,NULL,NULL),
(NULL,'TheBlackAlbum','1991-01-01',NULL,NULL,NULL),
(NULL,'WhiteAlbum','1968-01-01',NULL,NULL,NULL),
(NULL,'TheBlackAlbum','2003-01-01',NULL,NULL,NULL),
(NULL,'TheGreyAlbum','2004-01-01',NULL,NULL,NULL);
INSERT INTO Genre
VALUES
(NULL,'HeavyMetal'),
(NULL,'HeavyMetal'),
(NULL,'Pop'),
(NULL,'HipHop'),
(NULL,'HipHop');
SELECT
ArtistBand.BandName, Album.Name
FROM
ArtistBand
JOIN
Album
ORDER BY
ArtistBand.BandName;

You need to add condition on your join, that the ArtistID should be the same in both tables. So your query will look like below:
SELECT ArtistBand.BandName, Album.Name
FROM ArtistBand
JOIN Album ON ArtistBand.ArtistID = Album.ArtistID
ORDER BY ArtistBand.BandName;

Related

I need to JOIN using a linking table

The three tables are as follows:
CREATE TABLE Artist
(
ArtistKey char(20) NOT NULL PRIMARY KEY,
ArtistName varchar(50) NOT NULL
)
CREATE TABLE AlbumInfo
(
AlbumInfoKey char(20) NOT NULL PRIMARY KEY,
AlbumTitle varchar(50) NOT NULL,
AlbumDate date NULL,
AlbumStudio varchar(50) NULL
)
CREATE TABLE AlbumArtist
(
AlbumInfoKey char(20) NOT NULL,
ArtistKey char(20) NOT NULL,
PRIMARY KEY CLUSTERED
(
AlbumInfoKey ASC,
ArtistKey ASC
))
My objective is to list all of the artists and their albums. I can't seem to get anything to work.
I have tried:
SELECT
Artist.ArtistName,
AlbumInfo.AlbumTitle
FROM Artist
JOIN AlbumArtist
ON Artist.ArtistKey = AlbumArtist.ArtistKey
JOIN AlbumInfo
On AlbumInfo.AlbumInfoKey = AlbumArtist.AlbumInfoKey
However this gives me back nothing not even an error.
Alright, I had to re-do your whole task, and I have come up with more professional, and better way of managing database. You need to drop those tables, and re-do whole thing like show in code below :
--First create Artist table
CREATE TABLE Artist
(
Artist_key int PRIMARY KEY IDENTITY(1,1),
ArtistName varchar(50) NOT NULL,
);
--Then create Album table
CREATE TABLE AlbumInfo
(
Album_key int NOT NULL PRIMARY KEY IDENTITY(1,1),
AlbumTitle varchar(50) NOT NULL,
AlbumDate date NULL,
AlbumStudio varchar(50) NULL,
Artist_key int FOREIGN KEY (Artist_key) REFERENCES Artist(Artist_key)
);
-- Must have Artist data before referencing in the album table
INSERT into Artist (ArtistName) values ('John')
INSERT into AlbumInfo (AlbumTitle,AlbumDate,AlbumStudio,Artist_key) values ('ABC3','2020-6-12','Def3',(select Artist_key from Artist where Artist_key = 1 ))
--test if data has been inserted
SELECT * FROM Artist
SELECT * FROM AlbumInfo
-- And finally this query will show the Artist with their relevant Albums
SELECT ArtistName,af.AlbumTitle,AlbumStudio from Artist a join AlbumInfo af on af.Artist_key = a.Artist_key
And the result is :

List which of the most competing members from each category got the highest score

I have 4 tables:
User
create table user (
id int not null identity(1,1) primary key,
name varchar(30),
soyad varchar(50),
city varchar(30),
e_mail varchar(100),
pass varchar(10)
);
Category
create table category(
id int not null primary key,
name varchar(50)
);
Competition
create table competition(
id int primary key,
date date,
category_id int foreign key references category(id)
);
Competition_User
create table competition_user(
id int primary key,
competition_id int foreign key references competition(id),
joker_id int foreign key references joker(joker_id),
user_id int foreign key references user(id),
point int
);
I want write SQL code for this:
Write the SQL query, which will list which of the most competing members from each category got the highest score, in which category it competed, and how many questions are correct and how many questions are wrong.
I write this SQL query but failure:
SELECT category.id, user.id, count(competition_user.competition.id) AS competitioncount
FROM user, competition_user, competition, category
WHERE competition.id = competition_user.competition_id AND compettion.category_id = category.id AND user.id = competition.user_id
GROUP BY category.id, user.id
ORDER BY competitioncount desc

SQL: "foreign key constraint fails" error message

I get an error when attempting to insert data into the songs table. I'm not sure why.
Any help would be greatly appreciated. Thanks. This is me adding details so it will let me post haha.
create table artist
(
id int primary key auto_increment,
name varchar(128) not null,
nationality varchar(128)
) ENGINE=InnoDB;
insert into artist (name, nationality) values ('Metallica', 'American');
insert into artist (name, nationality) values ('Rush', 'Canadian');
create table album
(
id int primary key auto_increment,
name varchar (128) not null,
artist int not null,
foreign key (artist) references artist(id),
genre int,
foreign key (genre) references genre(id)
) ENGINE=InnoDB;
insert into album (name, artist, genre) values ('Ride the Lightning', 1, 1);
insert into album (name, artist, genre) values ('Moving Pictures', 2, 2);
create table song
(
id int primary key auto_increment,
name varchar (128) not null,
duration varchar (128),
album int not null,
foreign key (album) references album(id)
) ENGINE=InnoDB;
insert into song (name, duration, album) values ('Fade to Black', '1 min', 1);
insert into song (name, duration, album) values ('Tom Sawyer', '2 min', 2);
create table genre
(
id int primary key auto_increment,
name varchar (128) not null,
description varchar (256)
) ENGINE=InnoDB;
insert into genre (name, description) values ('Rock', 'Lots of drums and guitars');
insert into genre (name, description) values ('Metal', 'Drums and guitars on steroids');
The order of your code is causing the problem. You need to move the
create table genre
(
id int primary key auto_increment,
name varchar (128) not null,
description varchar (256)
) ENGINE=InnoDB;
insert into genre (name, description) values ('Rock', 'Lots of drums and guitars');
insert into genre (name, description) values ('Metal', 'Drums and guitars on steroids');
Up above the "Create table album"
Here is the correct way to do it: HOW TO DO IT
This is some reasons why it did not work:
There is no table "genre" when you try to create table album:
create table album
(
id int primary key auto_increment,
name varchar (128) not null,
artist int not null,
foreign key (artist) references artist(id),
genre int,
foreign key (genre) references genre(id)
) ENGINE=InnoDB;
Here is the DEMO
Also, when you create table "genre" on time, there is another problem. You have to insert data in table "genre" to be able to insert other data.
Here is the DEMO where all works.
One is the order of the create and insert statements shouldve given you error already as table referres doesnt exists etc.
Assuming if the order is rightly implemented, as per the above error not able to insert data in songs is likely because
Your album table has a null id or id which you are inserting in songs doesnt exists in album first insert the data in album table then insert into songs table as the foreign key of songs table is checking the album table for id data

SQL how can I get list of users and departments with filter by position?

Please help me, how can i get a list of users of the department with a filter by position?
CREATE TABLE COMPANY
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR(255),
PRIMARY KEY (ID),
UNIQUE KEY COMPANY_NAME (NAME)
);
CREATE TABLE USER
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR(255),
LASTNAME VARCHAR(255),
DATE_OF_BIRTH DATE ,
PRIMARY KEY (ID),
);
CREATE TABLE POSITION
(
ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
POSITION VARCHAR (50),
USERID INT NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (USERID) REFERENCES USER(ID)
);
CREATE TABLE DEPARTMENT
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
USER_ID INT NOT NULL,
COMPANY_ID INT NOT NULL,
DEPARTMENT_CATEGORY VARCHAR(50),
PRIMARY KEY (ID),
FOREIGN KEY (USER_ID) REFERENCES USER(ID),
FOREIGN KEY (COMPANY_ID) REFERENCES COMPANY(ID)
);
CREATE TABLE CATEGORY
(
ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR (50),
PRIMARY KEY (ID)
);
CREATE TABLE DEPARTMENT_CATEGORY
(
DEPARTMENT_ID INT NOT NULL,
CATEGORY_ID INT NOT NULL,
FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENT(ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY(ID)
);
Please help me!!
it look like you wanted to display results from department and position table that has both the data you need, but be specific about your filter (position) as u need to provide it in your where clause
select user_ID, department
from department as a
join user as b
on a. user_id = b. user_id
where position = 'put the specific position you wanted to be filtered here'
but your two userID columns are written differently as (User_id or USERID) correct that too not get failure result when you execute the task

SQL referencing foreign key

I can't get the GenreID foreign key on the Album table to work. Anyone know why?
CREATE TABLE ArtistBand
(
ArtistID INT AUTO_INCREMENT PRIMARY KEY,
BandName VARCHAR(255) NOT NULL,
NameOfMembers VARCHAR(255),
NumberOfMembers INT
);
CREATE TABLE Genre
(
GenreID INT AUTO_INCREMENT PRIMARY KEY,
GenreType VARCHAR(255) NOT NULL,
AlbumID INT,
FOREIGN KEY(AlbumID) REFERENCES Album(AlbumID)
);
CREATE TABLE Album
(
AlbumID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR (255) NOT NULL,
ReleaseDate DATE NOT NULL,
Producers VARCHAR (255),
ArtistID INT,
GenreID INT,
FOREIGN KEY(ArtistID) REFERENCES ArtistBand(ArtistID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);
Get rid of the circular foreign key reference between Genre and Album. Logically speaking, the reference from Genre to Album doesn't make any sense anyway :).
CREATE TABLE ArtistBand (
ArtistID INT AUTO_INCREMENT PRIMARY KEY,
BandName VARCHAR(255) NOT NULL,
NameOfMembers VARCHAR(255),
NumberOfMembers INT
);
CREATE TABLE Genre(
GenreID INT AUTO_INCREMENT PRIMARY KEY,
GenreType VARCHAR(255) NOT NULL
);
CREATE TABLE Album (
AlbumID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR (255) NOT NULL,
ReleaseDate DATE NOT NULL,
Producers VARCHAR (255),
ArtistID INT,
GenreID INT,
FOREIGN KEY(ArtistID) REFERENCES ArtistBand(ArtistID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);
if use sql server : auto_increment be modified to identity
CREATE TABLE ArtistBand (
ArtistID INT identity(1,1) PRIMARY KEY,
BandName VARCHAR(255) NOT NULL,
NameOfMembers VARCHAR(255),
NumberOfMembers INT
);
CREATE TABLE Genre(
GenreID INT identity(1,1) PRIMARY KEY,
GenreType VARCHAR(255) NOT NULL
);
CREATE TABLE Album (
AlbumID INT identity(1,1) PRIMARY KEY,
Name VARCHAR (255) NOT NULL,
ReleaseDate DATE NOT NULL,
Producers VARCHAR (255),
ArtistID INT,
GenreID INT,
FOREIGN KEY(ArtistID) REFERENCES ArtistBand(ArtistID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);