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

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

Related

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

"Incorrect syntax near 'DESCRIBE'. [41,1]" No other context, can't find syntax error

New to SQL, can't figure out what is wrong in my given code. all it says is:
Incorrect syntax near 'DESCRIBE'. [41,1]
I have tried taking off the semi-colons. I really just don't know what it wants from me.
Here is my code. Anything helps, thank you!
-- Write the query to create the 4 tables below.
CREATE TABLE client (
id INT NOT NULL IDENTITY(1,1),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT (full_name) UNIQUE (first_name, last_name)
);
CREATE TABLE employee (
id INT NOT NULL IDENTITY(1,1),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
date_joined DATE NOT NULL,
CONSTRAINT (full_name) UNIQUE (first_name, last_name),
PRIMARY KEY (id)
);
CREATE TABLE project (
id INT NOT NULL IDENTITY(1,1),
cid INT NOT NULL,
name VARCHAR(255) NOT NULL,
notes TEXT,
UNIQUE (name),
FOREIGN KEY (cid) REFERENCES client(id)
);
CREATE TABLE works_on (
eid INT NOT NULL,
pid INT NOT NULL,
start_date DATE NOT NULL,
PRIMARY KEY (eid, pid),
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id)
);
-- Leave the queries below untouched. These are to test your submission correctly.
-- Test that the tables were created
DESCRIBE client;
DESCRIBE employee;
DESCRIBE project;
DESCRIBE works_on;
-- Test that the correct foreign keys were created
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = 'grade';
For MariaDB (and MySQL) the correct syntax for IDENTITY(1,1), is AUTO_INCREMENT, and CONSTRAINT names are not enclosed in (). Any column that is defined as AUTO_INCREMENT must also be declared as a PRIMARY KEY (this is only an issue with the project table). So your CREATE TABLE commands should look like this:
CREATE TABLE client (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT full_name UNIQUE (first_name, last_name)
);
CREATE TABLE employee (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
date_joined DATE NOT NULL,
CONSTRAINT full_name UNIQUE (first_name, last_name),
PRIMARY KEY (id)
);
CREATE TABLE project (
id INT NOT NULL AUTO_INCREMENT,
cid INT NOT NULL,
name VARCHAR(255) NOT NULL,
notes TEXT,
PRIMARY KEY (id),
UNIQUE (name),
FOREIGN KEY (cid) REFERENCES client(id)
);
CREATE TABLE works_on (
eid INT NOT NULL,
pid INT NOT NULL,
start_date DATE NOT NULL,
PRIMARY KEY (eid, pid),
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id)
);
Demo on dbfiddle

How to properly configure database schema & inserts

So I have the following schema.
CREATE TABLE user_group (
id BIGINT PRIMARY KEY NOT NULL,
user_id BIGINT NOT NULL,
group_table_id BIGINT NOT NULL,
role VARCHAR(255),
CONSTRAINT user_group_user_id_fk FOREIGN KEY (user_id) REFERENCES user(id),
CONSTRAINT user_group_group_table_id_fk FOREIGN KEY (group_table_id) REFERENCES group_table(id)
);
CREATE TABLE group_table
(
id BIGINT PRIMARY KEY NOT NULL,
group_name VARCHAR(255),
group_picture_url VARCHAR(255),
tags VARCHAR(255),
description VARCHAR(255),
event_id BIGINT,
user_group_id BIGINT,
CONSTRAINT group_table_user_group_id_fk FOREIGN KEY (user_group_id) REFERENCES user_group(id)
);
CREATE TABLE user (
id BIGINT PRIMARY KEY NOT NULL,
display_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
picture_url VARCHAR(255),
user_group_id BIGINT,
event_response_id BIGINT,
CONSTRAINT user_user_group_id_fk FOREIGN KEY (user_group_id) REFERENCES user_group(id),
);
How can I make it so that this is a valid statement? In the sense that I should be able to create a user_group table, group_table table, or user table without needing the others.
The other question that I have is. Say this statement is valid and the tables exist. How would I insert data into user_group?
INSERT INTO user_group VALUES (1, 2, 3, 'role')
Would require that both a group_table and a user with id's of 2 and 3 respectively already exist upon insert of the value.
How can I make it so that this is a valid statement? In the sense that I should be able to create a user_group table, group_table table, or user table without needing the others.
You can create the tables first, and create the foreign key constraints after. If you try to do both at the same time (like you currently have), you run into a chicken and egg situation.
Here is an example of how you could do it:
CREATE TABLE user_group (
id BIGINT PRIMARY KEY NOT NULL,
user_id BIGINT NOT NULL,
group_table_id BIGINT NOT NULL,
role VARCHAR(255)
);
CREATE TABLE group_table
(
id BIGINT PRIMARY KEY NOT NULL,
group_name VARCHAR(255),
group_picture_url VARCHAR(255),
tags VARCHAR(255),
description VARCHAR(255),
event_id BIGINT,
user_group_id BIGINT
);
CREATE TABLE user (
id BIGINT PRIMARY KEY NOT NULL,
display_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
picture_url VARCHAR(255),
user_group_id BIGINT,
event_response_id BIGINT
);
alter table user_group
add constraint user_group_user_id_fk
foreign key (user_id)
references user(id);
alter table user_group
add constraint user_group_group_table_id_fk
foreign key (group_table_id)
REFERENCES group_table(id);
alter table group_table
add constraint group_table_user_group_id_fk
FOREIGN KEY (user_group_id)
REFERENCES user_group(id);
alter table user
add constraint user_user_group_id_fk
FOREIGN KEY (user_group_id)
REFERENCES user_group(id);
The other question that I have is. Say this statement is valid and the tables exist. How would I insert data into user_group?
INSERT INTO user_group VALUES (1, 2, 3, 'role')
Would require that both a group_table and a user with id's of 2 and 3 respectively already exist upon insert of the value.
Yes, the corresponding rows in user (id = 2) and group (id = 3) would have to be inserted prior to attempting the insert into user_group.

ERROR: No unique constraint matching given keys for referenced table

For some reason I'm getting an error* in my code. I'm quite new to PostgreSQL, and simply SQL. What is causing this error?
*there is no unique constraint matching given keys for referenced table "tech".
BEGIN;
CREATE TABLE Person (
person_id SERIAL PRIMARY KEY,
firstname VARCHAR(128),
lastname VARCHAR(128),
email_adr VARCHAR(128),
UNIQUE(person_id, email_adr)
);
CREATE TABLE Phone (
person_id INT REFERENCES Person(person_id),
phone_nr INT PRIMARY KEY,
UNIQUE(phone_nr)
);
CREATE TABLE Tech (
tech_id INT REFERENCES Person(person_id),
username VARCHAR(80) PRIMARY KEY,
password VARCHAR(80) NOT NULL,
location Varchar(128),
UNIQUE(username, tech_id)
);
CREATE TABLE Customer (
customer_id INT REFERENCES Persons(person_id),
addresse VARCHAR(255) NOT NULL,
UNIQUE(customer_id)
);
CREATE TABLE Task (
task_id SERIAL PRIMARY KEY,
payment MONEY,
tech INT REFERENCES Tech(tech_id) NOT NULL,
customer INT REFERENCES Customer(customer_id) NOT NULL,
start_date DATE NOT NULL,
end_dato DATE,
UNIQUE(tech, customer, start_date, end_date)
);
COMMIT;
In table Task you trying to reference to table Tech by tech_id. To do that you must add UNIQUE CONSTRAINT to tech_id in Tech.
Right now in table Tech you have UNIQUE(username, tech_id) that means that values in column tech_id could by doubled Ex.
Tech
-------------------------------
tech_id username, ....
------------------------------
1 'John'
2 'Tony'
1 'Nataly'
Acctually the better idea is to set reference by PRIMARY KEY, so in your case username in table Tech.
If you want to leave structure the way present in question, you should just add UNIQUE(tech_id) in column Tech.
What do you think of this code?
BEGIN;
CREATE TABLE Person (
person_id SERIAL PRIMARY KEY,
firstname VARCHAR(128),
lastname VARCHAR(128),
email_adr VARCHAR(128),
UNIQUE(person_id),
UNIQUE(email_adr)
);
CREATE TABLE Phone (
person_id INT,
phone_nr INT PRIMARY KEY,
);
CREATE TABLE Tech (
tech_id INT,
username VARCHAR(80) PRIMARY KEY,
password VARCHAR(80) NOT NULL,
location Varchar(128),
FOREIGN KEY(tech_id) REFERENCES Person(person_id),
UNIQUE(username),
UNIQUE(tech_id)
);
CREATE TABLE Customer (
customer_id INT REFERENCES Persons(person_id),
addresse VARCHAR(255) NOT NULL,
FOREIGN KEY(tech_id) REFERENCES Person(person_id),
UNIQUE(customer_id)
);
CREATE TABLE Task (
task_id SERIAL PRIMARY KEY,
payment MONEY,
tech varchar(80) REFERENCES Tech(username) NOT NULL,
customer INT REFERENCES Customer(customer_id) NOT NULL,
start_date DATE NOT NULL,
end_dato DATE,
UNIQUE(tech, customer, start_date, end_date)
);
COMMIT;

How to organize tables to stare statistic of two entities?

For now I have only two tables
CREATE TABLE IF NOT EXISTS company (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250) NOT NULL
);
CREATE TABLE IF NOT EXISTS employee (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250),
company_id INT,
FOREIGN KEY (company_id) REFERENCES Company (id)
);
I need to create one or two tables to store employees and companies statistic. For employee statistic I need to remember all previous companies of this employee and of cause hire dates and resign dates. For company statistic I need to remember all resigned employees. What is the best way to organize DB structure in my case?
Since you have many-to-many relationship, you need an aggregate table company_employee that will have combined primary key, so you need:
CREATE TABLE IF NOT EXISTS company (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250) NOT NULL
);
CREATE TABLE IF NOT EXISTS employee (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250)
);
CREATE TABLE IF NOT EXISTS company_employee (
company_id INT NOT NULL,
employee_id INT NOT NULL,
hire_date DATE,
resign_date DATE,
FOREIGN KEY (company_id) REFERENCES Company (id),
FOREIGN KEY (employee_id) REFERENCES Employee (id)
);
So, if you want anything from aggregate table, just use JOIN on key of appropriate table.