From cbb3fc3ed268156e50630e68db9b161bf3271ffc Mon Sep 17 00:00:00 2001 From: Harriery <98697353+Harriery@users.noreply.github.com> Date: Mon, 3 Mar 2025 19:44:02 +0100 Subject: [PATCH 1/2] foldes created --- Team1/Haluk/README.md | 32 ++++++++++++++++++++++++++++++++ Team1/Islam/README.md | 32 ++++++++++++++++++++++++++++++++ "Team1/Yas\304\261n/README.md" | 32 ++++++++++++++++++++++++++++++++ Team1/Zehra/README.md | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 Team1/Haluk/README.md create mode 100644 Team1/Islam/README.md create mode 100644 "Team1/Yas\304\261n/README.md" create mode 100644 Team1/Zehra/README.md diff --git a/Team1/Haluk/README.md b/Team1/Haluk/README.md new file mode 100644 index 0000000..52923d4 --- /dev/null +++ b/Team1/Haluk/README.md @@ -0,0 +1,32 @@ +# Database_Modul_Week_10 + +## TASK_1 + +- Additional Source: https://www.youtube.com/watch?v=miEFm1CyjfM&t=314s (in English) +* Before doing your Github homework, let's create the following tables in PostgreSQL using Python codes. Let's write the codes to run the queries. + +![image](https://github.com/user-attachments/assets/a56283aa-2e46-4cb0-86ec-eeb7837ed520) + + +![image](https://github.com/user-attachments/assets/e5be4d85-0781-48aa-b640-1b559be37de4) + + +1. Find the employees who get paid more than Rodney Weaver. +2. Find the average, min and max salaries +3. Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees. +4. Find the employees (first name, last name from employees table) who work under the Operations department (departments table). Our query should return first name and last name info. +5. Find the employees (first name, last name from employees table) who work under the Technology department (departments table). Our query should return first name and last name info. +6. Find the average salary of female employees. +7. Find the average salaries of each department. +8. Find the oldest and newest employees. +9. Find the hiring date and department of the highest paid employee +10. Find the hiring date and department of the lowest paid employee + +## TASK_2 + +You are expected to complete the mini project, which has a detailed explanation in the link below. + +https://werhere-it-academy.gitbook.io/werhere-it-academy-handbook/database-modulu/3.hafta/database-project-vit4 + + +### GoodLuck :) diff --git a/Team1/Islam/README.md b/Team1/Islam/README.md new file mode 100644 index 0000000..52923d4 --- /dev/null +++ b/Team1/Islam/README.md @@ -0,0 +1,32 @@ +# Database_Modul_Week_10 + +## TASK_1 + +- Additional Source: https://www.youtube.com/watch?v=miEFm1CyjfM&t=314s (in English) +* Before doing your Github homework, let's create the following tables in PostgreSQL using Python codes. Let's write the codes to run the queries. + +![image](https://github.com/user-attachments/assets/a56283aa-2e46-4cb0-86ec-eeb7837ed520) + + +![image](https://github.com/user-attachments/assets/e5be4d85-0781-48aa-b640-1b559be37de4) + + +1. Find the employees who get paid more than Rodney Weaver. +2. Find the average, min and max salaries +3. Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees. +4. Find the employees (first name, last name from employees table) who work under the Operations department (departments table). Our query should return first name and last name info. +5. Find the employees (first name, last name from employees table) who work under the Technology department (departments table). Our query should return first name and last name info. +6. Find the average salary of female employees. +7. Find the average salaries of each department. +8. Find the oldest and newest employees. +9. Find the hiring date and department of the highest paid employee +10. Find the hiring date and department of the lowest paid employee + +## TASK_2 + +You are expected to complete the mini project, which has a detailed explanation in the link below. + +https://werhere-it-academy.gitbook.io/werhere-it-academy-handbook/database-modulu/3.hafta/database-project-vit4 + + +### GoodLuck :) diff --git "a/Team1/Yas\304\261n/README.md" "b/Team1/Yas\304\261n/README.md" new file mode 100644 index 0000000..52923d4 --- /dev/null +++ "b/Team1/Yas\304\261n/README.md" @@ -0,0 +1,32 @@ +# Database_Modul_Week_10 + +## TASK_1 + +- Additional Source: https://www.youtube.com/watch?v=miEFm1CyjfM&t=314s (in English) +* Before doing your Github homework, let's create the following tables in PostgreSQL using Python codes. Let's write the codes to run the queries. + +![image](https://github.com/user-attachments/assets/a56283aa-2e46-4cb0-86ec-eeb7837ed520) + + +![image](https://github.com/user-attachments/assets/e5be4d85-0781-48aa-b640-1b559be37de4) + + +1. Find the employees who get paid more than Rodney Weaver. +2. Find the average, min and max salaries +3. Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees. +4. Find the employees (first name, last name from employees table) who work under the Operations department (departments table). Our query should return first name and last name info. +5. Find the employees (first name, last name from employees table) who work under the Technology department (departments table). Our query should return first name and last name info. +6. Find the average salary of female employees. +7. Find the average salaries of each department. +8. Find the oldest and newest employees. +9. Find the hiring date and department of the highest paid employee +10. Find the hiring date and department of the lowest paid employee + +## TASK_2 + +You are expected to complete the mini project, which has a detailed explanation in the link below. + +https://werhere-it-academy.gitbook.io/werhere-it-academy-handbook/database-modulu/3.hafta/database-project-vit4 + + +### GoodLuck :) diff --git a/Team1/Zehra/README.md b/Team1/Zehra/README.md new file mode 100644 index 0000000..52923d4 --- /dev/null +++ b/Team1/Zehra/README.md @@ -0,0 +1,32 @@ +# Database_Modul_Week_10 + +## TASK_1 + +- Additional Source: https://www.youtube.com/watch?v=miEFm1CyjfM&t=314s (in English) +* Before doing your Github homework, let's create the following tables in PostgreSQL using Python codes. Let's write the codes to run the queries. + +![image](https://github.com/user-attachments/assets/a56283aa-2e46-4cb0-86ec-eeb7837ed520) + + +![image](https://github.com/user-attachments/assets/e5be4d85-0781-48aa-b640-1b559be37de4) + + +1. Find the employees who get paid more than Rodney Weaver. +2. Find the average, min and max salaries +3. Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees. +4. Find the employees (first name, last name from employees table) who work under the Operations department (departments table). Our query should return first name and last name info. +5. Find the employees (first name, last name from employees table) who work under the Technology department (departments table). Our query should return first name and last name info. +6. Find the average salary of female employees. +7. Find the average salaries of each department. +8. Find the oldest and newest employees. +9. Find the hiring date and department of the highest paid employee +10. Find the hiring date and department of the lowest paid employee + +## TASK_2 + +You are expected to complete the mini project, which has a detailed explanation in the link below. + +https://werhere-it-academy.gitbook.io/werhere-it-academy-handbook/database-modulu/3.hafta/database-project-vit4 + + +### GoodLuck :) From 339ca612627702c708416faeb03471c5dbbd354b Mon Sep 17 00:00:00 2001 From: Harriery <98697353+Harriery@users.noreply.github.com> Date: Sat, 8 Mar 2025 00:01:22 +0100 Subject: [PATCH 2/2] database_week_10 --- Team1/Haluk/homework.py | 64 ++++++++++++ Team1/Islam/Homework.py | 185 ++++++++++++++++++++++++++++++++++ "Team1/Yas\304\261n/hw.py" | 185 ++++++++++++++++++++++++++++++++++ "Team1/Yas\304\261n/odev.py" | 72 +++++++++++++ "Team1/Yas\304\261n/odev.sql" | 52 ++++++++++ Team1/Zehra/homework.py | 185 ++++++++++++++++++++++++++++++++++ 6 files changed, 743 insertions(+) create mode 100644 Team1/Haluk/homework.py create mode 100644 Team1/Islam/Homework.py create mode 100644 "Team1/Yas\304\261n/hw.py" create mode 100644 "Team1/Yas\304\261n/odev.py" create mode 100644 "Team1/Yas\304\261n/odev.sql" create mode 100644 Team1/Zehra/homework.py diff --git a/Team1/Haluk/homework.py b/Team1/Haluk/homework.py new file mode 100644 index 0000000..f5f4acd --- /dev/null +++ b/Team1/Haluk/homework.py @@ -0,0 +1,64 @@ +import psycopg2 + +conn = psycopg2.connect(host='localhost',dbname='postgres',user='postgres',password='344230haluk',port=5432) + +cur = conn.cursor() + +# DEPARTMENTS_TABLE + +cur.execute("""CREATE TABLE IF NOT EXISTS departments_table( + emp_id INT PRIMARY KEY, + dept_name VARCHAR(100), + dept_id INT +); +""") + +cur.execute("""INSERT INTO departments_table (emp_id,dept_name,dept_id) VALUES +(17679, 'Operations', 13), +(26650, 'Marketing', 14), +(30840, 'Operations', 13), +(49823, 'Technology', 12), +(51821, 'Operations', 13), +(67323, 'Marketing', 14), +(71119, 'Administrative', 11), +(76589, 'Operations', 13), +(97927, 'Technology', 12) +ON CONFLICT (emp_id) DO NOTHING; +""") + + + + + + + +# EMPLOYEES_TABLE OLUSTURULAN YER +cur.execute("""CREATE TABLE IF NOT EXISTS employees_table( + emp_id INT PRIMARY KEY, + firs_name VARCHAR(100), + last_name VARCHAR(100), + salary INT, + job_title VARCHAR(200), + gender VARCHAR(20), + hire_date DATE +); +""") + +cur.execute("""INSERT INTO employees_table (emp_id,firs_name,last_name,salary,job_title,gender,hire_date) VALUES +(17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', '2018-09-04'), +(26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', '2017-11-24'), +(30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', '2019-12-02'), +(49714, 'Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male', '2019-11-22'), +(51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', '2019-04-29'), +(67323, 'Lisa', 'Wiener', 75000, 'Business Analyst', 'Female', '2018-08-09'), +(70950, 'Rodney', 'Weaver', 87000, 'Project Manager', 'Male', '2018-12-20'), +(71329, 'Gayle', 'Meyer', 77000, 'HR Manager', 'Female', '2019-06-28'), +(76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', '2019-01-21'), +(97927, 'Billie', 'Lanning', 67000, 'Web Developer', 'Female', '2018-06-25') +ON CONFLICT (emp_id) DO NOTHING; +""") + + +conn.commit() +cur.close() +conn.close() \ No newline at end of file diff --git a/Team1/Islam/Homework.py b/Team1/Islam/Homework.py new file mode 100644 index 0000000..cbfb004 --- /dev/null +++ b/Team1/Islam/Homework.py @@ -0,0 +1,185 @@ +import psycopg2 + +# 1. Önce 'postgres' veritabanına bağlan ve yeni veritabanını oluştur +baglan = psycopg2.connect( + dbname='postgres', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) + +baglan.autocommit = True # CREATE DATABASE için autocommit açık olmalı +cursor = baglan.cursor() + +# 'odev' veritabanı varsa oluşturma +cursor.execute("SELECT 1 FROM pg_database WHERE datname = 'odev';") +db_var_mi = cursor.fetchone() + +if not db_var_mi: + cursor.execute("CREATE DATABASE odev;") +else: + print("Veritabanı zaten var, oluşturulmadı.") + +#1️⃣ pg_database, sistemdeki tüm veritabanlarının kayıtlı olduğu bir tablodur. +#2️⃣ datname = 'odev', bu tablo içinde "odev" adında bir veritabanı olup olmadığını kontrol eder. +#3️⃣ SELECT 1, sadece "odev" varsa 1 döndürmesini sağlar (gereksiz veri çekmemek için). + +#📌 Eğer "odev" veritabanı varsa, sonuç olarak 1 döner. +#📌 Eğer yoksa, sonuç boş (None) olur. + + +cursor.close() +baglan.close() + +# 2. Yeni oluşturduğun 'odev' veritabanına bağlan +baglan = psycopg2.connect( + dbname='odev', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) + +cursor = baglan.cursor() + +# 3. 'employees' tablosunu oluştur +cursor.execute(""" + CREATE TABLE IF NOT EXISTS employees ( + emp_id SERIAL PRIMARY KEY, + first_name VARCHAR(100), + last_name VARCHAR(100), + salary INT, + job_title VARCHAR(200), + gender VARCHAR(20), + hire_date DATE + ); +""") + + +cursor.execute(""" + INSERT INTO employees (emp_id,first_name,last_name,salary,job_title,gender,hire_date) VALUES + (17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', '2018-09-04'), + (26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', '2017-11-24'), + (30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', '2019-12-02'), + (49714, 'Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male', '2019-11-22'), + (51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', '2019-04-29'), + (67323, 'Lisa', 'Wiener', 75000, 'Business Analyst', 'Female', '2018-08-09'), + (70950, 'Rodney', 'Weaver', 87000, 'Project Manager', 'Male', '2018-12-20'), + (71329, 'Gayle', 'Meyer', 77000, 'HR Manager', 'Female', '2019-06-28'), + (76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', '2019-01-21'), + (97927, 'Billie', 'Lanning', 67000, 'Web Developer', 'Female', '2018-06-25') + ON CONFLICT (emp_id) DO NOTHING; +""") + +cursor.execute(""" + CREATE TABLE IF NOT EXISTS departments_table( + emp_id INT PRIMARY KEY, + dept_name VARCHAR(100), + dept_id INT +); +""") + +cursor.execute(""" + INSERT INTO departments_table (emp_id,dept_name,dept_id) VALUES + (17679, 'Operations', 13), + (26650, 'Marketing', 14), + (30840, 'Operations', 13), + (49823, 'Technology', 12), + (51821, 'Operations', 13), + (67323, 'Marketing', 14), + (71119, 'Administrative', 11), + (76589, 'Operations', 13), + (97927, 'Technology', 12) + ON CONFLICT (emp_id) DO NOTHING; +""") +# 1- Find the employees who get paid more than Rodney Weaver. +cursor.execute(""" + SELECT first_name, last_name, salary + FROM employees + WHERE salary > 87000; +""") +## print(cursor.fetchone()) + +# 2- Find the average, min and max salaries +cursor.execute(""" + SELECT MIN(salary), MAX(salary), AVG(salary) + FROM employees; +""") +#print(cursor.fetchone()) + +# 3- Find the employees whose salary is more than 8700. +# Our query should return first name, last name, and salary info of the employees. +cursor.execute(""" + SELECT first_name, last_name, salary + FROM employees + WHERE salary > 87000; +""") +# print(cursor.fetchone()) + +# 4- Find the employees (first name, last name from employees table) who work under the Operations department (departments table). +# Our query should return first name and last name info. +cursor.execute(""" + SELECT first_name, last_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE dept_name = 'Operations'; +""") +#print(cursor.fetchone()) + +# 5- Find the employees (first name, last name from employees table) who work under the Technology department (departments table). +# Our query should return first name and last name info. +cursor.execute(""" + SELECT first_name, last_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE dept_name = 'Technology'; +""") +#print(cursor.fetchone()) + +# 6- Find the average salary of female employees. +cursor.execute(""" + SELECT AVG(salary) + FROM employees + WHERE gender = 'Female'; +""") +#print(cursor.fetchone()) + +# 7- Find the average salaries of each department. +cursor.execute(""" + SELECT departments_table.dept_name, AVG(salary) + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + GROUP BY departments_table.dept_name; +""") +#print(cursor.fetchone()) + +# 8- Find the oldest and newest employees. +cursor.execute(""" +SELECT MIN(hire_date) AS oldest_hire, MAX(hire_date) AS newest_hire +FROM employees; +""") +#print(cursor.fetchone()) + +# 9- Find the hiring date and department of the highest paid employee +cursor.execute(""" + SELECT employees.hire_date, departments_table.dept_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE employees.salary = (SELECT MAX(salary) FROM employees); +""") +# print(cursor.fetchone()) + +# 10-Find the hiring date and department of the lowest paid employee +cursor.execute(""" + SELECT employees.hire_date, departments_table.dept_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE employees.salary = (SELECT MIN (salary) FROM employees) + """) + + + +baglan.commit() +cursor.close() +baglan.close() diff --git "a/Team1/Yas\304\261n/hw.py" "b/Team1/Yas\304\261n/hw.py" new file mode 100644 index 0000000..cbfb004 --- /dev/null +++ "b/Team1/Yas\304\261n/hw.py" @@ -0,0 +1,185 @@ +import psycopg2 + +# 1. Önce 'postgres' veritabanına bağlan ve yeni veritabanını oluştur +baglan = psycopg2.connect( + dbname='postgres', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) + +baglan.autocommit = True # CREATE DATABASE için autocommit açık olmalı +cursor = baglan.cursor() + +# 'odev' veritabanı varsa oluşturma +cursor.execute("SELECT 1 FROM pg_database WHERE datname = 'odev';") +db_var_mi = cursor.fetchone() + +if not db_var_mi: + cursor.execute("CREATE DATABASE odev;") +else: + print("Veritabanı zaten var, oluşturulmadı.") + +#1️⃣ pg_database, sistemdeki tüm veritabanlarının kayıtlı olduğu bir tablodur. +#2️⃣ datname = 'odev', bu tablo içinde "odev" adında bir veritabanı olup olmadığını kontrol eder. +#3️⃣ SELECT 1, sadece "odev" varsa 1 döndürmesini sağlar (gereksiz veri çekmemek için). + +#📌 Eğer "odev" veritabanı varsa, sonuç olarak 1 döner. +#📌 Eğer yoksa, sonuç boş (None) olur. + + +cursor.close() +baglan.close() + +# 2. Yeni oluşturduğun 'odev' veritabanına bağlan +baglan = psycopg2.connect( + dbname='odev', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) + +cursor = baglan.cursor() + +# 3. 'employees' tablosunu oluştur +cursor.execute(""" + CREATE TABLE IF NOT EXISTS employees ( + emp_id SERIAL PRIMARY KEY, + first_name VARCHAR(100), + last_name VARCHAR(100), + salary INT, + job_title VARCHAR(200), + gender VARCHAR(20), + hire_date DATE + ); +""") + + +cursor.execute(""" + INSERT INTO employees (emp_id,first_name,last_name,salary,job_title,gender,hire_date) VALUES + (17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', '2018-09-04'), + (26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', '2017-11-24'), + (30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', '2019-12-02'), + (49714, 'Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male', '2019-11-22'), + (51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', '2019-04-29'), + (67323, 'Lisa', 'Wiener', 75000, 'Business Analyst', 'Female', '2018-08-09'), + (70950, 'Rodney', 'Weaver', 87000, 'Project Manager', 'Male', '2018-12-20'), + (71329, 'Gayle', 'Meyer', 77000, 'HR Manager', 'Female', '2019-06-28'), + (76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', '2019-01-21'), + (97927, 'Billie', 'Lanning', 67000, 'Web Developer', 'Female', '2018-06-25') + ON CONFLICT (emp_id) DO NOTHING; +""") + +cursor.execute(""" + CREATE TABLE IF NOT EXISTS departments_table( + emp_id INT PRIMARY KEY, + dept_name VARCHAR(100), + dept_id INT +); +""") + +cursor.execute(""" + INSERT INTO departments_table (emp_id,dept_name,dept_id) VALUES + (17679, 'Operations', 13), + (26650, 'Marketing', 14), + (30840, 'Operations', 13), + (49823, 'Technology', 12), + (51821, 'Operations', 13), + (67323, 'Marketing', 14), + (71119, 'Administrative', 11), + (76589, 'Operations', 13), + (97927, 'Technology', 12) + ON CONFLICT (emp_id) DO NOTHING; +""") +# 1- Find the employees who get paid more than Rodney Weaver. +cursor.execute(""" + SELECT first_name, last_name, salary + FROM employees + WHERE salary > 87000; +""") +## print(cursor.fetchone()) + +# 2- Find the average, min and max salaries +cursor.execute(""" + SELECT MIN(salary), MAX(salary), AVG(salary) + FROM employees; +""") +#print(cursor.fetchone()) + +# 3- Find the employees whose salary is more than 8700. +# Our query should return first name, last name, and salary info of the employees. +cursor.execute(""" + SELECT first_name, last_name, salary + FROM employees + WHERE salary > 87000; +""") +# print(cursor.fetchone()) + +# 4- Find the employees (first name, last name from employees table) who work under the Operations department (departments table). +# Our query should return first name and last name info. +cursor.execute(""" + SELECT first_name, last_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE dept_name = 'Operations'; +""") +#print(cursor.fetchone()) + +# 5- Find the employees (first name, last name from employees table) who work under the Technology department (departments table). +# Our query should return first name and last name info. +cursor.execute(""" + SELECT first_name, last_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE dept_name = 'Technology'; +""") +#print(cursor.fetchone()) + +# 6- Find the average salary of female employees. +cursor.execute(""" + SELECT AVG(salary) + FROM employees + WHERE gender = 'Female'; +""") +#print(cursor.fetchone()) + +# 7- Find the average salaries of each department. +cursor.execute(""" + SELECT departments_table.dept_name, AVG(salary) + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + GROUP BY departments_table.dept_name; +""") +#print(cursor.fetchone()) + +# 8- Find the oldest and newest employees. +cursor.execute(""" +SELECT MIN(hire_date) AS oldest_hire, MAX(hire_date) AS newest_hire +FROM employees; +""") +#print(cursor.fetchone()) + +# 9- Find the hiring date and department of the highest paid employee +cursor.execute(""" + SELECT employees.hire_date, departments_table.dept_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE employees.salary = (SELECT MAX(salary) FROM employees); +""") +# print(cursor.fetchone()) + +# 10-Find the hiring date and department of the lowest paid employee +cursor.execute(""" + SELECT employees.hire_date, departments_table.dept_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE employees.salary = (SELECT MIN (salary) FROM employees) + """) + + + +baglan.commit() +cursor.close() +baglan.close() diff --git "a/Team1/Yas\304\261n/odev.py" "b/Team1/Yas\304\261n/odev.py" new file mode 100644 index 0000000..fd71298 --- /dev/null +++ "b/Team1/Yas\304\261n/odev.py" @@ -0,0 +1,72 @@ +import psycopg2 + +baglan = psycopg2.connect( + dbname= 'postgres', + user= 'postgres', + password='411.Falcon', + host='localhost', + port='5432' +) +baglan.autocommit = True +cursor = baglan.cursor() +baglan.close() + +# Yeni veri tabanına bağlan +baglan = psycopg2.connect( + dbname='benim_veritabanim', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) +cursor = baglan.cursor() + +cursor.execute(""" + CREATE TABLE IF NOT EXISTS employees ( + emp_id SERIAL PRIMARY KEY, + first_name VARCHAR(50) NOT NULL, + last_name VARCHAR(50) NOT NULL, + salary INT NOT NULL, + job_title VARCHAR(100) NOT NULL, + gender VARCHAR(20) NOT NULL CHECK(gender IN ('Female', 'Male')), + hire_date DATE DEFAULT CURRENT_DATE + ); +""") +cursor.execute("""INSERT INTO employees (first_name, last_name, salary, job_title, gender) VALUES +('Robert', 'Gilmore', 110000, 'Operation Director', 'Male' ), +('Elvis', 'Ritter', 86000, 'Sales Manager', 'Male' ), +('David', 'Barrow', 85000, 'Data Scientist', 'Male' ), +('Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male' ), +('Linda', 'Foster', 95000, 'Data Scientist', 'Female' ), +('Lisa', 'Wiener', 75000, 'Business Analyst', 'Female' ), +('Rodney', 'Weaver', 87000, 'Project Manager', 'Male' ), +('Gayle', 'Meyer', 77000, 'HR Manager', 'Female' ), +('Jason', 'Christian', 99000, 'Project Manager', 'Male' ), +('Billie', 'Lanning', 67000, 'Web Developer', 'Female' ) +ON CONFLICT (emp_id) DO NOTHING; +""") + +cursor.execute(""" + CREATE TABLE IF NOT EXISTS departments ( + dept_id SERIAL PRIMARY KEY, + dept_name VARCHAR(50) NOT NULL UNIQUE + + ); +""") + + +cursor.execute(""" + INSERT INTO departments (dept_name) VALUES + ('Operations'), + ('Marketing'), + ('Technology'), + ('Administrative') + ON CONFLICT ( dept_id) DO NOTHING; +""") + + + +baglan.commit() + +cursor.close() +baglan.close() \ No newline at end of file diff --git "a/Team1/Yas\304\261n/odev.sql" "b/Team1/Yas\304\261n/odev.sql" new file mode 100644 index 0000000..7f12393 --- /dev/null +++ "b/Team1/Yas\304\261n/odev.sql" @@ -0,0 +1,52 @@ +--1 +SELECT first_name, last_name, salary +FROM employees +WHERE salary > 87000; + +--2 +SELECT MIN(salary), MAX(salary), AVG(salary) +FROM employees; + +--3 +SELECT first_name, last_name, salary +FROM employees +WHERE salary > 87000; + +--4 +SELECT first_name, last_name +FROM employees +INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id +WHERE dept_name = 'Operations'; + +--5 +SELECT first_name, last_name +FROM employees +INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id +WHERE dept_name = 'Technology'; + +--6 +SELECT AVG(salary) +FROM employees +WHERE gender = 'Female'; + +--7 +SELECT departments_table.dept_name, AVG(salary) +FROM employees +INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id +GROUP BY departments_table.dept_name; + +--8 +SELECT MIN(hire_date) AS oldest_hire, MAX(hire_date) AS newest_hire +FROM employees; + +--9 +SELECT employees.hire_date, departments_table.dept_name +FROM employees +INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id +WHERE employees.salary = (SELECT MAX(salary) FROM employees); + +--10 +SELECT employees.hire_date, departments_table.dept_name +FROM employees +INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id +WHERE employees.salary = (SELECT MIN (salary) FROM employees) diff --git a/Team1/Zehra/homework.py b/Team1/Zehra/homework.py new file mode 100644 index 0000000..cbfb004 --- /dev/null +++ b/Team1/Zehra/homework.py @@ -0,0 +1,185 @@ +import psycopg2 + +# 1. Önce 'postgres' veritabanına bağlan ve yeni veritabanını oluştur +baglan = psycopg2.connect( + dbname='postgres', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) + +baglan.autocommit = True # CREATE DATABASE için autocommit açık olmalı +cursor = baglan.cursor() + +# 'odev' veritabanı varsa oluşturma +cursor.execute("SELECT 1 FROM pg_database WHERE datname = 'odev';") +db_var_mi = cursor.fetchone() + +if not db_var_mi: + cursor.execute("CREATE DATABASE odev;") +else: + print("Veritabanı zaten var, oluşturulmadı.") + +#1️⃣ pg_database, sistemdeki tüm veritabanlarının kayıtlı olduğu bir tablodur. +#2️⃣ datname = 'odev', bu tablo içinde "odev" adında bir veritabanı olup olmadığını kontrol eder. +#3️⃣ SELECT 1, sadece "odev" varsa 1 döndürmesini sağlar (gereksiz veri çekmemek için). + +#📌 Eğer "odev" veritabanı varsa, sonuç olarak 1 döner. +#📌 Eğer yoksa, sonuç boş (None) olur. + + +cursor.close() +baglan.close() + +# 2. Yeni oluşturduğun 'odev' veritabanına bağlan +baglan = psycopg2.connect( + dbname='odev', + user='postgres', + password='411.Falcon', + host='localhost', + port='5432' +) + +cursor = baglan.cursor() + +# 3. 'employees' tablosunu oluştur +cursor.execute(""" + CREATE TABLE IF NOT EXISTS employees ( + emp_id SERIAL PRIMARY KEY, + first_name VARCHAR(100), + last_name VARCHAR(100), + salary INT, + job_title VARCHAR(200), + gender VARCHAR(20), + hire_date DATE + ); +""") + + +cursor.execute(""" + INSERT INTO employees (emp_id,first_name,last_name,salary,job_title,gender,hire_date) VALUES + (17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', '2018-09-04'), + (26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', '2017-11-24'), + (30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', '2019-12-02'), + (49714, 'Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male', '2019-11-22'), + (51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', '2019-04-29'), + (67323, 'Lisa', 'Wiener', 75000, 'Business Analyst', 'Female', '2018-08-09'), + (70950, 'Rodney', 'Weaver', 87000, 'Project Manager', 'Male', '2018-12-20'), + (71329, 'Gayle', 'Meyer', 77000, 'HR Manager', 'Female', '2019-06-28'), + (76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', '2019-01-21'), + (97927, 'Billie', 'Lanning', 67000, 'Web Developer', 'Female', '2018-06-25') + ON CONFLICT (emp_id) DO NOTHING; +""") + +cursor.execute(""" + CREATE TABLE IF NOT EXISTS departments_table( + emp_id INT PRIMARY KEY, + dept_name VARCHAR(100), + dept_id INT +); +""") + +cursor.execute(""" + INSERT INTO departments_table (emp_id,dept_name,dept_id) VALUES + (17679, 'Operations', 13), + (26650, 'Marketing', 14), + (30840, 'Operations', 13), + (49823, 'Technology', 12), + (51821, 'Operations', 13), + (67323, 'Marketing', 14), + (71119, 'Administrative', 11), + (76589, 'Operations', 13), + (97927, 'Technology', 12) + ON CONFLICT (emp_id) DO NOTHING; +""") +# 1- Find the employees who get paid more than Rodney Weaver. +cursor.execute(""" + SELECT first_name, last_name, salary + FROM employees + WHERE salary > 87000; +""") +## print(cursor.fetchone()) + +# 2- Find the average, min and max salaries +cursor.execute(""" + SELECT MIN(salary), MAX(salary), AVG(salary) + FROM employees; +""") +#print(cursor.fetchone()) + +# 3- Find the employees whose salary is more than 8700. +# Our query should return first name, last name, and salary info of the employees. +cursor.execute(""" + SELECT first_name, last_name, salary + FROM employees + WHERE salary > 87000; +""") +# print(cursor.fetchone()) + +# 4- Find the employees (first name, last name from employees table) who work under the Operations department (departments table). +# Our query should return first name and last name info. +cursor.execute(""" + SELECT first_name, last_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE dept_name = 'Operations'; +""") +#print(cursor.fetchone()) + +# 5- Find the employees (first name, last name from employees table) who work under the Technology department (departments table). +# Our query should return first name and last name info. +cursor.execute(""" + SELECT first_name, last_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE dept_name = 'Technology'; +""") +#print(cursor.fetchone()) + +# 6- Find the average salary of female employees. +cursor.execute(""" + SELECT AVG(salary) + FROM employees + WHERE gender = 'Female'; +""") +#print(cursor.fetchone()) + +# 7- Find the average salaries of each department. +cursor.execute(""" + SELECT departments_table.dept_name, AVG(salary) + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + GROUP BY departments_table.dept_name; +""") +#print(cursor.fetchone()) + +# 8- Find the oldest and newest employees. +cursor.execute(""" +SELECT MIN(hire_date) AS oldest_hire, MAX(hire_date) AS newest_hire +FROM employees; +""") +#print(cursor.fetchone()) + +# 9- Find the hiring date and department of the highest paid employee +cursor.execute(""" + SELECT employees.hire_date, departments_table.dept_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE employees.salary = (SELECT MAX(salary) FROM employees); +""") +# print(cursor.fetchone()) + +# 10-Find the hiring date and department of the lowest paid employee +cursor.execute(""" + SELECT employees.hire_date, departments_table.dept_name + FROM employees + INNER JOIN departments_table ON employees.emp_id = departments_table.emp_id + WHERE employees.salary = (SELECT MIN (salary) FROM employees) + """) + + + +baglan.commit() +cursor.close() +baglan.close()