diff --git a/Ali.ipynb b/Ali.ipynb new file mode 100644 index 0000000..8e51cb8 --- /dev/null +++ b/Ali.ipynb @@ -0,0 +1,640 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ccfde88c", + "metadata": {}, + "source": [ + "Database ve tabloların oluşturulup kayıtların eklenmesi" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "603feaef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bağlantı başarılı!\n", + "Tablolar başarıyla oluşturuldu.\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "# bilgilerimle baglanti kuruyorum.\n", + "con = psycopg2.connect(host=\"localhost\", database= \"company_db\", user= \"postgres\", password= \"Istanbul1235.\", port= \"5432\")\n", + "print(\"Bağlantı başarılı!\")\n", + "\n", + "# # baglantimla bir cursor olusturuyorum.\n", + "cur = con.cursor()\n", + "\n", + "# veritabanimda bulunan tablolarin isimlerini aliyorum.\n", + "cur.execute(\"\"\"CREATE TABLE IF NOT EXISTS departments (\n", + " emp_id INT PRIMARY KEY,\n", + " dept_name VARCHAR(100),\n", + " dept_id INT \n", + ");\n", + "\"\"\")\n", + "\n", + "\n", + "\n", + "cur.execute(\"\"\"CREATE TABLE IF NOT EXISTS employees (\n", + " emp_id INT PRIMARY KEY,\n", + " first_name VARCHAR(100),\n", + " last_name VARCHAR(100),\n", + " salary NUMERIC(10, 2),\n", + " job_title VARCHAR(100),\n", + " gender VARCHAR(10),\n", + " hire_date DATE\n", + ");\n", + "\"\"\")\n", + "\n", + "cur.execute(\"\"\"\n", + " INSERT INTO departments (emp_id, dept_name, dept_id) VALUES\n", + " (17679, 'Operations', 13),\n", + " (26650, 'Marketing', 14),\n", + " (30840, 'Operations', 13),\n", + " (49823, 'Technology', 12),\n", + " (51821, 'Operations', 13),\n", + " (67323, 'Marketing', 14),\n", + " (71119, 'Administrative', 11),\n", + " (76589, 'Operations', 13),\n", + " (97927, 'Technology', 12); \n", + "\"\"\")\n", + "\n", + "cur.execute(\"\"\"\n", + " INSERT INTO employees (emp_id, first_name, last_name, salary, job_title, gender, hire_date) VALUES\n", + " (17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', '2018-09-04'),\n", + " (26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', '2017-11-24'),\n", + " (30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', '2019-12-02'),\n", + " (49714, 'Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male', '2019-11-22'),\n", + " (51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', '2019-04-29'),\n", + " (67323, 'Lisa', 'Wiener', 75000, 'Business Analyst', 'Female', '2018-08-09'),\n", + " (70950, 'Rodney', 'Weaver', 87000, 'Project Manager', 'Male', '2018-12-20'),\n", + " (71329, 'Gayle', 'Meyer', 77000, 'HR Manager', 'Female', '2019-06-28'),\n", + " (76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', '2019-01-21'),\n", + " (97927, 'Billie', 'Lanning', 67000, 'Web Developer', 'Female', '2018-06-25');\n", + "\"\"\")\n", + "\n", + "con.commit()\n", + "\n", + "print(\"Tablolar başarıyla oluşturuldu.\")\n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "98eef3aa", + "metadata": {}, + "source": [ + "1 - Find the employees who get paid more than Rodney Weaver." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f3ccbad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore', Decimal('110000.00'))\n", + "('Linda', 'Foster', Decimal('95000.00'))\n", + "('Jason', 'Christian', Decimal('99000.00'))\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "# Her sorguda bu bağlantı tekrar kurulmalı\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# ---------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT first_name, last_name, salary FROM employees\n", + "WHERE salary > (\n", + " SELECT salary\n", + " FROM employees\n", + " WHERE first_name = 'Rodney' AND last_name = 'Weaver'\n", + ");\n", + "\"\"\")\n", + "\n", + "rows = cur.fetchall()\n", + "for row in rows:\n", + " print(row)\n", + "# -------------------------------------------------------------\n", + "cur.close()\n", + "conn.close()\n" + ] + }, + { + "cell_type": "markdown", + "id": "0cc3d918", + "metadata": {}, + "source": [ + "2 - Find the average, min and max salaries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb7d3b7a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ortalama Maaş: 83600.00\n", + "En Düşük Maaş: 55000.00\n", + "En Yüksek Maaş: 110000.00\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "# Her sorguda bu bağlantı tekrar kurulmalı\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT AVG(salary), MIN(salary), MAX(salary) FROM employees;\n", + "\"\"\")\n", + "\n", + "result= cur.fetchone()\n", + "print(\"Ortalama Maaş:\", round(result[0],2))\n", + "print(\"En Düşük Maaş:\", result[1])\n", + "print(\"En Yüksek Maaş:\", result[2])\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "441acbe5", + "metadata": {}, + "source": [ + "3 - Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad5339d6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore', Decimal('110000.00'))\n", + "('Linda', 'Foster', Decimal('95000.00'))\n", + "('Jason', 'Christian', Decimal('99000.00'))\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "# Her sorguda bu bağlantı tekrar kurulmalı\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT first_name, last_name, salary FROM employees\n", + "WHERE salary > 87000;\n", + "\"\"\")\n", + "rows = cur.fetchall()\n", + "for row in rows:\n", + " print(row)\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()" + ] + }, + { + "cell_type": "markdown", + "id": "1185cde6", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b915a1f3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore')\n", + "('David', 'Barrow')\n", + "('Linda', 'Foster')\n", + "('Jason', 'Christian')\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "# Her sorguda bu bağlantı tekrar kurulmalı\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT e.first_name, e.last_name FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE d.dept_name = 'Operations';\n", + "\"\"\")\n", + "rows = cur.fetchall()\n", + "for row in rows:\n", + " print(row)\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()" + ] + }, + { + "cell_type": "markdown", + "id": "68db64e1", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "c520fe98", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Billie', 'Lanning')\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT e.first_name, e.last_name FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE d.dept_name = 'Technology';\n", + "\"\"\")\n", + "rows = cur.fetchall()\n", + "for row in rows:\n", + " print(row)\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()\n" + ] + }, + { + "cell_type": "markdown", + "id": "69cbf637", + "metadata": {}, + "source": [ + "6 - Find the average salary of female employees." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "d063513c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Kadın çalışanların ortalama maaşı: 78500.00\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT AVG(salary) FROM employees\n", + "WHERE gender = 'Female';\n", + "\"\"\")\n", + "avarage = cur.fetchone()[0]\n", + "print(\"Kadın çalışanların ortalama maaşı:\", round(avarage, 2))\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()" + ] + }, + { + "cell_type": "markdown", + "id": "2daebe7c", + "metadata": {}, + "source": [ + "7 - Find the average salaries of each department." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "fd6b9a41", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Departman: Technology | Ortalama Maaş: 67000.00\n", + "Departman: Marketing | Ortalama Maaş: 80500.00\n", + "Departman: Operations | Ortalama Maaş: 97250.00\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT d.dept_name, AVG(e.salary) FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "GROUP BY d.dept_name;\n", + "\"\"\")\n", + "rows = cur.fetchall()\n", + "for row in rows:\n", + " print(\"Departman:\", row[0], \"| Ortalama Maaş:\", round(row[1], 2))\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()" + ] + }, + { + "cell_type": "markdown", + "id": "56d2045d", + "metadata": {}, + "source": [ + "8 - Find the oldest and newest employees." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "dda1a998", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "En eski calisan: ('Elvis', 'Ritter', datetime.date(2017, 11, 24))\n", + "En yeni calisan: ('David', 'Barrow', datetime.date(2019, 12, 2))\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT first_name, last_name, hire_date FROM employees\n", + "ORDER BY hire_date ASC\n", + "LIMIT 1;\n", + "\"\"\")\n", + "oldest = cur.fetchone()\n", + "print(\"En eski calisan:\", oldest)\n", + "\n", + "cur.execute(\"\"\"\n", + "SELECT first_name, last_name, hire_date FROM employees\n", + "ORDER BY hire_date DESC\n", + "LIMIT 1;\n", + "\"\"\")\n", + "newest = cur.fetchone()\n", + "print(\"En yeni calisan:\", newest)\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()" + ] + }, + { + "cell_type": "markdown", + "id": "95de0e03", + "metadata": {}, + "source": [ + "9 - Find the hiring date and department of the highest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "fea4e9aa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "En yüksek maaş alan çalışan:\n", + "İsim: Robert Gilmore\n", + "İşe Başlama Tarihi: 2018-09-04\n", + "Departman: Operations\n", + "Maaş: 110000.00\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT e.first_name, e.last_name, e.hire_date, d.dept_name , e.salary\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE e.salary = (\n", + " SELECT MAX(salary) FROM employees\n", + ");\n", + "\"\"\")\n", + "\n", + "highest_paid = cur.fetchone()\n", + "print(\"En yüksek maaş alan çalışan:\")\n", + "print(\"İsim:\", highest_paid[0], highest_paid[1])\n", + "print(\"İşe Başlama Tarihi:\", highest_paid[2])\n", + "print(\"Departman:\", highest_paid[3])\n", + "print(\"Maaş:\", highest_paid[4])\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()\n" + ] + }, + { + "cell_type": "markdown", + "id": "5f6a39a7", + "metadata": {}, + "source": [ + "10 - Find the hiring date and department of the lowest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24704580", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "En düşük maaş alan çalışan:\n", + "İsim: Robert Gilmore\n", + "İşe Başlama Tarihi: 2018-09-04\n", + "Departman: Operations\n", + "Maaş: 110000.00\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "\n", + "conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " port=\"5432\",\n", + " database=\"company_db\",\n", + " user=\"postgres\",\n", + " password=\"Istanbul1235.\"\n", + ")\n", + "\n", + "cur = conn.cursor()\n", + "# -------------------------------------------------------------\n", + "cur.execute(\"\"\"\n", + "SELECT e.first_name, e.last_name, e.hire_date, d.dept_name , e.salary\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE e.salary = (\n", + " SELECT MAX(salary) FROM employees\n", + ");\n", + "\"\"\")\n", + "\n", + "lowest_paid = cur.fetchone()\n", + "print(\"En düşük maaş alan çalışan:\")\n", + "print(\"İsim:\", lowest_paid[0], lowest_paid[1])\n", + "print(\"İşe Başlama Tarihi:\", lowest_paid[2])\n", + "print(\"Departman:\", lowest_paid[3])\n", + "print(\"Maaş:\", lowest_paid[4])\n", + "# -------------------------------------------------------------\n", + "cur.close() \n", + "conn.close()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Furkan.ipynb b/Furkan.ipynb new file mode 100644 index 0000000..9b71ec9 --- /dev/null +++ b/Furkan.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ccfde88c", + "metadata": {}, + "source": [ + "Database ve tabloların oluşturulup kayıtların eklenmesi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "603feaef", + "metadata": {}, + "outputs": [], + "source": [ + "import sqlite3\n", + "import pandas as pd\n", + "\n", + "conn = sqlite3.connect('company.db')\n", + "\n", + "conn.execute(\"\"\"\n", + "CREATE TABLE IF NOT EXISTS departments (\n", + " id INTEGER PRIMARY KEY,\n", + " name TEXT\n", + ")\n", + "\"\"\")\n", + "conn.execute(\"\"\"\n", + "CREATE TABLE IF NOT EXISTS employees (\n", + " id INTEGER PRIMARY KEY,\n", + " first_name TEXT,\n", + " last_name TEXT,\n", + " gender TEXT,\n", + " salary INTEGER,\n", + " hire_date TEXT,\n", + " department_id INTEGER,\n", + " FOREIGN KEY(department_id) REFERENCES departments(id)\n", + ")\n", + "\"\"\")\n", + "conn.execute(\"INSERT OR IGNORE INTO departments (id, name) VALUES (1, 'Operations'), (2, 'Technology')\")\n", + "conn.execute(\"\"\"\n", + "INSERT OR IGNORE INTO employees (id, first_name, last_name, gender, salary, hire_date, department_id) VALUES\n", + "(1, 'Rodney', 'Weaver', 'M', 8000, '2015-06-01', 1),\n", + "(2, 'Alice', 'Smith', 'F', 9000, '2017-03-15', 2),\n", + "(3, 'Bob', 'Johnson', 'M', 9500, '2018-07-22', 1),\n", + "(4, 'Carol', 'Williams', 'F', 8700, '2019-11-30', 2)\n", + "\"\"\")\n", + "conn.commit()\n" + ] + }, + { + "cell_type": "markdown", + "id": "98eef3aa", + "metadata": {}, + "source": [ + "1 - Find the employees who get paid more than Rodney Weaver." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f3ccbad", + "metadata": {}, + "outputs": [], + "source": [ + "import sqlite3\n", + "import pandas as pd\n", + "\n", + "conn = sqlite3.connect('company.db')\n", + "\n", + "salary = pd.read_sql(\"SELECT salary FROM employees WHERE first_name='Rodney' AND last_name='Weaver'\", conn).iloc[0,0]\n", + "df = pd.read_sql(f\"SELECT first_name, last_name, salary FROM employees WHERE salary > {salary}\", conn)\n", + "df\n" + ] + }, + { + "cell_type": "markdown", + "id": "0cc3d918", + "metadata": {}, + "source": [ + "2 - Find the average, min and max salaries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb7d3b7a", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"SELECT AVG(salary) as avg_salary, MIN(salary) as min_salary, MAX(salary) as max_salary FROM employees\", conn)\n", + "df\n" + ] + }, + { + "cell_type": "markdown", + "id": "441acbe5", + "metadata": {}, + "source": [ + "3 - Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad5339d6", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"SELECT first_name, last_name, salary FROM employees WHERE salary > 8700\", conn)\n", + "df\n" + ] + }, + { + "cell_type": "markdown", + "id": "1185cde6", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b915a1f3", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"\"\"\n", + "SELECT e.first_name, e.last_name FROM employees e\n", + "JOIN departments d ON e.department_id = d.id\n", + "WHERE d.name = 'Operations'\n", + "\"\"\", conn)\n" + ] + }, + { + "cell_type": "markdown", + "id": "68db64e1", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c520fe98", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"\"\"\n", + "SELECT e.first_name, e.last_name FROM employees e\n", + "JOIN departments d ON e.department_id = d.id\n", + "WHERE d.name = 'Technology'\n", + "\"\"\", conn)\n" + ] + }, + { + "cell_type": "markdown", + "id": "69cbf637", + "metadata": {}, + "source": [ + "6 - Find the average salary of female employees." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d063513c", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"SELECT AVG(salary) as avg_female_salary FROM employees WHERE gender = 'F'\", conn)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "id": "2daebe7c", + "metadata": {}, + "source": [ + "7 - Find the average salaries of each department." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fd6b9a41", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"\"\"\n", + "SELECT d.name as department, AVG(e.salary) as avg_salary\n", + "FROM employees e\n", + "JOIN departments d ON e.department_id = d.id\n", + "GROUP BY d.name\n", + "\"\"\"," + ] + }, + { + "cell_type": "markdown", + "id": "56d2045d", + "metadata": {}, + "source": [ + "8 - Find the oldest and newest employees." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dda1a998", + "metadata": {}, + "outputs": [], + "source": [ + "df_oldest = pd.read_sql(\"SELECT first_name, last_name, hire_date FROM employees ORDER BY hire_date ASC LIMIT 1\", conn)\n", + "df_newest = pd.read_sql(\"SELECT first_name, last_name, hire_date FROM employees ORDER BY hire_date DESC LIMIT 1\", conn)" + ] + }, + { + "cell_type": "markdown", + "id": "95de0e03", + "metadata": {}, + "source": [ + "9 - Find the hiring date and department of the highest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fea4e9aa", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"\"\"\n", + "SELECT e.hire_date, d.name as department FROM employees e\n", + "JOIN departments d ON e.department_id = d.id\n", + "WHERE e.salary = (SELECT MAX(salary) FROM employees)\n", + "\"\"\", conn)" + ] + }, + { + "cell_type": "markdown", + "id": "5f6a39a7", + "metadata": {}, + "source": [ + "10 - Find the hiring date and department of the lowest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24704580", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_sql(\"\"\"\n", + "SELECT e.hire_date, d.name as department FROM employees e\n", + "JOIN departments d ON e.department_id = d.id\n", + "WHERE e.salary = (SELECT MIN(salary) FROM employees)\n", + "\"\"\"," + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Kursad.ipynb b/Kursad.ipynb new file mode 100644 index 0000000..dc71705 --- /dev/null +++ b/Kursad.ipynb @@ -0,0 +1,790 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ccfde88c", + "metadata": {}, + "source": [ + "Database ve tabloların oluşturulup kayıtların eklenmesi" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "603feaef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "company veritabanı zaten mevcut.\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT\n", + "\n", + "# Bağlantı bilgileri\n", + "credentials = {\n", + " \"dbname\": \"company\",\n", + " \"user\": \"postgres\",\n", + " \"password\": \"12345\",\n", + " \"host\": \"localhost\",\n", + " \"port\": \"5432\"\n", + "}\n", + "\n", + "def create_database_if_not_exists(creds):\n", + " # Önce postgres veritabanına bağlan\n", + " conn = psycopg2.connect(\n", + " dbname=\"postgres\",\n", + " user=creds[\"user\"],\n", + " password=creds[\"password\"],\n", + " host=creds[\"host\"],\n", + " port=creds[\"port\"]\n", + " )\n", + " conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)\n", + " cur = conn.cursor()\n", + " try:\n", + " cur.execute(f\"CREATE DATABASE {creds['dbname']};\")\n", + " print(f\"{creds['dbname']} veritabanı oluşturuldu.\")\n", + " except psycopg2.errors.DuplicateDatabase:\n", + " print(f\"{creds['dbname']} veritabanı zaten mevcut.\")\n", + " finally:\n", + " cur.close()\n", + " conn.close()\n", + "\n", + "def create_tables_and_insert_data(creds):\n", + " conn = psycopg2.connect(**creds)\n", + " cur = conn.cursor()\n", + "\n", + " # Tablo oluşturma\n", + " cur.execute(\"\"\"\n", + " CREATE TABLE IF NOT EXISTS departments (\n", + " emp_id SERIAL PRIMARY KEY,\n", + " dept_name VARCHAR(50) NOT NULL,\n", + " dept_id INTEGER NOT NULL\n", + " );\n", + " \"\"\")\n", + " cur.execute(\"\"\"\n", + " CREATE TABLE IF NOT EXISTS employees (\n", + " emp_id INTEGER PRIMARY KEY,\n", + " first_name VARCHAR(50),\n", + " last_name VARCHAR(50),\n", + " salary INTEGER,\n", + " job_title VARCHAR(100),\n", + " gender VARCHAR(10),\n", + " hire_date DATE NOT NULL\n", + " );\n", + " \"\"\")\n", + "\n", + " # departments verileri\n", + " departments_data = [\n", + " (17679, \"Operations\", 13),\n", + " (26650, \"Marketing\", 14),\n", + " (30840, \"Operations\", 13),\n", + " (49823, \"Technology\", 12),\n", + " (51821, \"Operations\", 13),\n", + " (67323, \"Marketing\", 14),\n", + " (71119, \"Administrative\", 11),\n", + " (76589, \"Operations\", 13),\n", + " (97927, \"Technology\", 12)\n", + " ]\n", + "\n", + " # employees verileri\n", + " employees_data = [\n", + " (17679, \"Robert\", \"Gilmore\", 110000, \"Operations Director\", \"Male\", \"2018-09-04\"),\n", + " (26650, \"Elvis\", \"Ritter\", 86000, \"Sales Manager\", \"Male\", \"2017-11-24\"),\n", + " (30840, \"David\", \"Barrow\", 85000, \"Data Scientist\", \"Male\", \"2019-12-02\"),\n", + " (49714, \"Hugo\", \"Forester\", 55000, \"IT Support Specialist\", \"Male\", \"2019-11-22\"),\n", + " (51821, \"Linda\", \"Foster\", 95000, \"Data Scientist\", \"Female\", \"2019-04-29\"),\n", + " (67323, \"Lisa\", \"Wiener\", 75000, \"Business Analyst\", \"Female\", \"2018-08-09\"),\n", + " (70950, \"Rodney\", \"Weaver\", 87000, \"Project Manager\", \"Male\", \"2018-12-20\"),\n", + " (71329, \"Gayle\", \"Meyer\", 77000, \"HR Manager\", \"Female\", \"2019-06-28\"),\n", + " (76589, \"Jason\", \"Christian\", 99000, \"Project Manager\", \"Male\", \"2019-01-21\"),\n", + " (97927, \"Billie\", \"Lanning\", 67000, \"Web Developer\", \"Female\", \"2018-06-25\")\n", + " ]\n", + "\n", + " # Kayıt ekleme (varsa hata vermemesi için ON CONFLICT DO NOTHING)\n", + " cur.executemany(\"\"\"\n", + " INSERT INTO departments (emp_id, dept_name, dept_id)\n", + " VALUES (%s, %s, %s)\n", + " ON CONFLICT (emp_id) DO NOTHING;\n", + " \"\"\", departments_data)\n", + "\n", + " cur.executemany(\"\"\"\n", + " INSERT INTO employees (emp_id, first_name, last_name, salary, job_title, gender, hire_date)\n", + " VALUES (%s, %s, %s, %s, %s, %s, %s)\n", + " ON CONFLICT (emp_id) DO NOTHING;\n", + " \"\"\", employees_data)\n", + "\n", + " conn.commit()\n", + " cur.close()\n", + " conn.close()\n", + "\n", + "create_database_if_not_exists(credentials)\n", + "create_tables_and_insert_data(credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "2ce60e2d", + "metadata": {}, + "source": [ + "SQL Fetchall" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "0e2b22e5", + "metadata": {}, + "outputs": [], + "source": [ + "def get_data_list(creds, query, params=None):\n", + " \"\"\"Verilen sorguyu çalıştırır ve sonuçları döndürür.\"\"\"\n", + " import psycopg2\n", + " results = []\n", + " try:\n", + " with psycopg2.connect(**creds) as conn:\n", + " with conn.cursor() as cur:\n", + " cur.execute(query, params or ())\n", + " results = cur.fetchall()\n", + " except Exception as e:\n", + " print(\"Veritabanı hatası:\", e)\n", + " return results" + ] + }, + { + "cell_type": "markdown", + "id": "1f03c8cc", + "metadata": {}, + "source": [ + "SQL Fetchone" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "8566fbc1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_data(creds, query, params=None):\n", + " \"\"\"Verilen sorguyu çalıştırır ve sonuçları döndürür.\"\"\"\n", + " import psycopg2\n", + " results = []\n", + " try:\n", + " with psycopg2.connect(**creds) as conn:\n", + " with conn.cursor() as cur:\n", + " cur.execute(query, params or ())\n", + " results = cur.fetchone()\n", + " except Exception as e:\n", + " print(\"Veritabanı hatası:\", e)\n", + " return results" + ] + }, + { + "cell_type": "markdown", + "id": "98eef3aa", + "metadata": {}, + "source": [ + "1 - Find the employees who get paid more than Rodney Weaver." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f3ccbad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore', 110000)\n", + "('Jason', 'Christian', 99000)\n", + "('Linda', 'Foster', 95000)\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT e.first_name, e.last_name, e.salary\n", + " FROM employees e\n", + " CROSS JOIN (\n", + " SELECT salary FROM employees\n", + " WHERE first_name = %s AND last_name = %s\n", + " LIMIT 1\n", + " ) r\n", + " WHERE e.salary > r.salary\n", + " ORDER BY e.salary DESC;\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query, ('Rodney', 'Weaver'))\n", + "\n", + "for row in results:\n", + " print(row)\n" + ] + }, + { + "cell_type": "markdown", + "id": "0cc3d918", + "metadata": {}, + "source": [ + "2 - Find the average, min and max salaries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb7d3b7a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ortalama Maaş: 83600 \n", + "Maksimum Maaş: 110000 \n", + "Minimum Maaş: 55000\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT \n", + " CAST(ROUND(AVG(salary),0) AS INTEGER) AS average_salary,\n", + " MAX(salary) AS maximum_salary,\n", + " MIN(salary) AS minimum_salary\n", + " FROM employees;\n", + "\"\"\"\n", + "\n", + "results = get_data(credentials, sql_query)\n", + "\n", + "if results:\n", + " print(f\"Ortalama Maaş: {results[0]} \\nMaksimum Maaş: {results[1]} \\nMinimum Maaş: {results[2]}\")" + ] + }, + { + "cell_type": "markdown", + "id": "441acbe5", + "metadata": {}, + "source": [ + "3 - Find the employees whose salary is more than 87000. Our query should return first name, last name, and salary info of the employees." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad5339d6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Robert Gilmore 110000\n", + "Jason Christian 99000\n", + "Linda Foster 95000\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT e.first_name, e.last_name, e.salary\n", + " FROM employees e\n", + " WHERE e.salary > 87000\n", + " ORDER BY e.salary DESC;\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query, ('Rodney', 'Weaver'))\n", + "\n", + "for row in results:\n", + " print(row[0] +\" \"+ row[1], row[2])" + ] + }, + { + "cell_type": "markdown", + "id": "1185cde6", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b915a1f3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Robert Gilmore\n", + "David Barrow\n", + "Linda Foster\n", + "Jason Christian\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT e.first_name, e.last_name\n", + " FROM employees e\n", + " JOIN departments d ON e.emp_id = d.emp_id\n", + " WHERE d.dept_name = %s;\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query, (\"Operations\",))\n", + "\n", + "for row in results:\n", + " print(row[0] +\" \"+ row[1])" + ] + }, + { + "cell_type": "markdown", + "id": "68db64e1", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c520fe98", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Billie Lanning\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT e.first_name, e.last_name\n", + " FROM employees e\n", + " JOIN departments d ON e.emp_id = d.emp_id\n", + " WHERE d.dept_name = %s;\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query, (\"Technology\",))\n", + "\n", + "for row in results:\n", + " print(row[0] +\" \"+ row[1])" + ] + }, + { + "cell_type": "markdown", + "id": "69cbf637", + "metadata": {}, + "source": [ + "6 - Find the average salary of female employees." + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "d063513c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Averege salery of women: 78500 \n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT \n", + " CAST(ROUND(AVG(salary),0) AS INTEGER) AS average_salary\n", + " FROM employees\n", + " WHERE gender = %s; \n", + "\"\"\"\n", + "\n", + "results = get_data(credentials, sql_query, ('Female',))\n", + "\n", + "if results:\n", + " print(f\"Averege salery of women: {results[0]} \")" + ] + }, + { + "cell_type": "markdown", + "id": "2daebe7c", + "metadata": {}, + "source": [ + "7 - Find the average salaries of each department." + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "fd6b9a41", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Technology \t 67000\n", + "Marketing \t 80500\n", + "Operations \t 97250\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT \n", + " d.dept_name, CAST(ROUND(AVG(e.salary),0) AS INTEGER) AS average_salary\n", + " FROM employees e\n", + " JOIN departments d ON e.emp_id = d.emp_id\n", + " GROUP BY d.dept_name;\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query)\n", + "\n", + "for row in results:\n", + " print(f\"{row[0]} \\t {row[1]}\")" + ] + }, + { + "cell_type": "markdown", + "id": "56d2045d", + "metadata": {}, + "source": [ + "8 - Find the oldest and newest employees." + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "dda1a998", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elvis Ritter 2017-11-24\n", + "David Barrow 2019-12-02\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT first_name, last_name, hire_date\n", + " FROM employees\n", + " WHERE hire_date = (SELECT MIN(hire_date) FROM employees)\n", + " OR hire_date = (SELECT MAX(hire_date) FROM employees);\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query)\n", + "\n", + "for row in results:\n", + " print(row[0] +\" \"+ row[1], row[2])" + ] + }, + { + "cell_type": "markdown", + "id": "95de0e03", + "metadata": {}, + "source": [ + "9 - Find the hiring date and department of the highest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "fea4e9aa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Name: Robert Gilmore\n", + "Hire Date: 2018-09-04 \n", + "Department: Operations\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT e.first_name, e.last_name, e.hire_date, d.dept_name\n", + " FROM employees e\n", + " JOIN departments d ON e.emp_id = d.emp_id\n", + " WHERE e.salary = (SELECT MAX(salary) FROM employees);\n", + "\"\"\"\n", + "\n", + "results = get_data(credentials, sql_query)\n", + "\n", + "print(f\"Name: {results[0]} {results[1]}\\nHire Date: {results[2]} \\nDepartment: {results[3]}\")" + ] + }, + { + "cell_type": "markdown", + "id": "5f6a39a7", + "metadata": {}, + "source": [ + "10 - Find the hiring date and department of the lowest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "24704580", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Name: Billie Lanning\n", + "Hire Date: 2018-06-25 \n", + "Department: Technology\n" + ] + } + ], + "source": [ + "sql_query = \"\"\"\n", + " SELECT e.first_name, e.last_name, e.hire_date, d.dept_name\n", + " FROM employees e\n", + " JOIN departments d ON e.emp_id = d.emp_id\n", + " WHERE e.salary = (\n", + " SELECT MIN(e2.salary)\n", + " FROM employees e2\n", + " JOIN departments d2 ON e2.emp_id = d2.emp_id\n", + " );\n", + "\"\"\"\n", + "\n", + "results = get_data_list(credentials, sql_query)\n", + "\n", + "for row in results:\n", + " print(f\"Name: {row[0]} {row[1]}\\nHire Date: {row[2]} \\nDepartment: {row[3]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b513cb53", + "metadata": {}, + "outputs": [], + "source": [ + "import csv\n", + "import os\n", + "import psycopg2\n", + "\n", + "def read_csv_columns(file_path, columns):\n", + " \"\"\"\n", + " Belirtilen CSV dosyasından, istenen sütunlardaki verileri tablo olarak döndürür.\n", + " :param file_path: CSV dosyasının yolu\n", + " :param columns: Almak istediğiniz sütun adlarının listesi\n", + " :return: Seçilen sütunlardan oluşan satırların listesi\n", + " \"\"\"\n", + " results = []\n", + " with open(file_path, newline='', encoding='utf-8') as csvfile:\n", + " reader = csv.DictReader(csvfile)\n", + " for row in reader:\n", + " selected = [row[col] for col in columns]\n", + " results.append(selected)\n", + " return results\n", + "\n", + "\n", + "def insert_kursiyerler(creds, data):\n", + " \"\"\"\n", + " data: [['Ad Soyad', 'Mail', 'Telefon', 'Posta Kodu', 'Eyalet'], ...]\n", + " \"\"\"\n", + " conn = psycopg2.connect(**creds)\n", + " cur = conn.cursor()\n", + " insert_query = \"\"\"\n", + " INSERT INTO kursiyerler (AdSoyad, MailAdresi, TelefonNumarasi, PostaKodu, YasadiginizEyalet)\n", + " VALUES (%s, %s, %s, %s, %s)\n", + " ON CONFLICT DO NOTHING;\n", + " \"\"\"\n", + " for row in data:\n", + " cur.execute(insert_query, row)\n", + " conn.commit()\n", + " cur.close()\n", + " conn.close()\n", + "\n", + "# Kullanım örneği:\n", + "# Önce CSV'den verileri oku\n", + "file_path = 'Basvurular - Sayfa1.csv'\n", + "columns = [\"Adınız Soyadınız\", \"Mail adresiniz\", \"Telefon Numaranız\", \"Posta Kodunuz\", \"Yaşadığınız Eyalet\"]\n", + "liste = read_csv_columns(file_path, columns)\n", + "\n", + "# Veritabanı bağlantı bilgileri\n", + "creds = {\n", + " \"dbname\": \"wearehere\",\n", + " \"user\": \"postgres\",\n", + " \"password\": \"12345\",\n", + " \"host\": \"localhost\",\n", + " \"port\": \"5432\"\n", + "}\n", + "\n", + "insert_kursiyerler(creds, liste)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf68b866", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'ZamanDamgasi'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[17]\u001b[39m\u001b[32m, line 78\u001b[39m\n\u001b[32m 76\u001b[39m \u001b[38;5;66;03m# Kullanım örneği:\u001b[39;00m\n\u001b[32m 77\u001b[39m file_path = \u001b[33m\"\u001b[39m\u001b[33mBasvurular - Sayfa1.csv\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m---> \u001b[39m\u001b[32m78\u001b[39m liste = \u001b[43mread_csv_for_basvurular\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 80\u001b[39m creds = {\n\u001b[32m 81\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mdbname\u001b[39m\u001b[33m\"\u001b[39m: \u001b[33m\"\u001b[39m\u001b[33mwearehere\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m 82\u001b[39m \u001b[33m\"\u001b[39m\u001b[33muser\u001b[39m\u001b[33m\"\u001b[39m: \u001b[33m\"\u001b[39m\u001b[33mpostgres\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 85\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mport\u001b[39m\u001b[33m\"\u001b[39m: \u001b[33m\"\u001b[39m\u001b[33m5432\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 86\u001b[39m }\n\u001b[32m 88\u001b[39m insert_basvurular(creds, liste)\n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[17]\u001b[39m\u001b[32m, line 34\u001b[39m, in \u001b[36mread_csv_for_basvurular\u001b[39m\u001b[34m(file_path)\u001b[39m\n\u001b[32m 30\u001b[39m kursiyer_id = \u001b[32m1\u001b[39m\n\u001b[32m 31\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m row \u001b[38;5;129;01min\u001b[39;00m reader:\n\u001b[32m 32\u001b[39m data = [\n\u001b[32m 33\u001b[39m kursiyer_id,\n\u001b[32m---> \u001b[39m\u001b[32m34\u001b[39m parse_zaman_damgasi(\u001b[43mrow\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mZamanDamgasi\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m), \u001b[38;5;66;03m# Standart formata çevir\u001b[39;00m\n\u001b[32m 35\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33msuankidurum\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 36\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mitphegitimkatilmak\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 37\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mekonomikdurum\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 38\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mdilkursunadevam\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 39\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mingilizceseviye\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 40\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mhollandacaseviye\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 41\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mbaskigoruyor\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 42\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mbootcampbitirdi\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 43\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33monlineitkursu\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 44\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mittecrube\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 45\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mprojedahil\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 46\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mcalismakistegi\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 47\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mnedenkatilmakistiyor\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 48\u001b[39m row[\u001b[33m\"\u001b[39m\u001b[33mbasvurudonemi\u001b[39m\u001b[33m\"\u001b[39m],\n\u001b[32m 49\u001b[39m ]\n\u001b[32m 50\u001b[39m results.append(data)\n\u001b[32m 51\u001b[39m kursiyer_id += \u001b[32m1\u001b[39m\n", + "\u001b[31mKeyError\u001b[39m: 'ZamanDamgasi'" + ] + } + ], + "source": [ + "import csv\n", + "import psycopg2\n", + "from datetime import datetime\n", + "\n", + "def parse_zaman_damgasi(zaman_str):\n", + " \"\"\"\n", + " Farklı formatlardaki zaman damgalarını YYYY-MM-DD HH:MM:SS formatına çevirir.\n", + " \"\"\"\n", + " zaman_str = zaman_str.strip()\n", + " formats = [\n", + " \"%m/%d/%Y %H:%M:%S\", # 4/30/2023 21:38:19\n", + " \"%d.%m.%y %H:%M\", # 05.01.23 01:39\n", + " \"%d.%m.%y %H:%M:%S\", # 05.01.23 01:39:00 (varsa)\n", + " ]\n", + " for fmt in formats:\n", + " try:\n", + " return datetime.strptime(zaman_str, fmt).strftime(\"%Y-%m-%d %H:%M:%S\")\n", + " except ValueError:\n", + " continue\n", + " raise ValueError(f\"ZamanDamgasi formatı tanınmadı: {zaman_str}\")\n", + "\n", + "def read_csv_for_basvurular(file_path):\n", + " \"\"\"\n", + " CSV'den Basvurular tablosuna uygun verileri okur ve sıralı KursiyerID ekler.\n", + " ZamanDamgasi alanını standart formata çevirir.\n", + " \"\"\"\n", + " results = []\n", + " with open(file_path, newline='', encoding='utf-8') as csvfile:\n", + " reader = csv.DictReader(csvfile, delimiter='\\t', skipinitialspace=True)\n", + " kursiyer_id = 1\n", + " for row in reader:\n", + " data = [\n", + " kursiyer_id,\n", + " parse_zaman_damgasi(row[\"zamandamgasi\"]), # Standart formata çevir\n", + " row[\"suankidurum\"],\n", + " row[\"itphegitimkatilmak\"],\n", + " row[\"ekonomikdurum\"],\n", + " row[\"dilkursunadevam\"],\n", + " row[\"ingilizceseviye\"],\n", + " row[\"hollandacaseviye\"],\n", + " row[\"baskigoruyor\"],\n", + " row[\"bootcampbitirdi\"],\n", + " row[\"onlineitkursu\"],\n", + " row[\"ittecrube\"],\n", + " row[\"projedahil\"],\n", + " row[\"calismakistegi\"],\n", + " row[\"nedenkatilmakistiyor\"],\n", + " row[\"basvurudonemi\"],\n", + " ]\n", + " results.append(data)\n", + " kursiyer_id += 1\n", + " return results\n", + "\n", + "def insert_basvurular(creds, data):\n", + " \"\"\"\n", + " Basvurular tablosuna verileri ekler.\n", + " \"\"\"\n", + " conn = psycopg2.connect(**creds)\n", + " cur = conn.cursor()\n", + " insert_query = \"\"\"\n", + " INSERT INTO Basvurular (\n", + " KursiyerID, ZamanDamgasi, SuAnkiDurum, ITPHEgitimKatilmak, EkonomikDurum,\n", + " DilKursunaDevam, IngilizceSeviye, HollandacaSeviye, BaskiGoruyor,\n", + " BootcampBitirdi, OnlineITKursu, ITTecrube, ProjeDahil, CalismakIstegi,\n", + " NedenKatilmakIstiyor, BasvuruDonemi\n", + " )\n", + " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", + " ON CONFLICT DO NOTHING;\n", + " \"\"\"\n", + " for row in data:\n", + " cur.execute(insert_query, row)\n", + " conn.commit()\n", + " cur.close()\n", + " conn.close()\n", + "\n", + "# Kullanım örneği:\n", + "file_path = \"Basvurular - Sayfa1.csv\"\n", + "liste = read_csv_for_basvurular(file_path)\n", + "\n", + "creds = {\n", + " \"dbname\": \"wearehere\",\n", + " \"user\": \"postgres\",\n", + " \"password\": \"12345\",\n", + " \"host\": \"localhost\",\n", + " \"port\": \"5432\"\n", + "}\n", + "\n", + "insert_basvurular(creds, liste)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "6beb2c6c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ZamanDamgasi,Adınız Soyadınız,Mail adresiniz,Telefon Numaranız,Posta Kodunuz,Yaşadığınız Eyalet,suankidurum,itphegitimkatilmak,ekonomikdurum,dilkursunadevam,ingilizceseviye,hollandacaseviye,baskigoruyor,bootcampbitirdi,onlineitkursu,ittecrube,projedahil,calismakistegi,nedenkatilmakistiyor,basvurudonemi,mentorgorusmesi']\n" + ] + } + ], + "source": [ + "import csv\n", + "\n", + "with open(\"Basvurular - Sayfa1.csv\", newline='', encoding='utf-8') as csvfile:\n", + " reader = csv.DictReader(csvfile, delimiter='\\t')\n", + " print(reader.fieldnames)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/L\303\274tf\303\274.ipynb" "b/L\303\274tf\303\274.ipynb" new file mode 100644 index 0000000..a7aa924 --- /dev/null +++ "b/L\303\274tf\303\274.ipynb" @@ -0,0 +1,555 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ccfde88c", + "metadata": {}, + "source": [ + "Database ve tabloların oluşturulup kayıtların eklenmesi" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "603feaef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "company veritabanı zaten mevcut.\n", + "Tablolar oluşturuldu ve veriler eklendi.\n" + ] + } + ], + "source": [ + "import psycopg2\n", + "from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT\n", + "\n", + "\n", + "# Veritabanı bağlantı bilgileri\n", + "credentials = {\n", + " \"dbname\": \"company\",\n", + " \"user\": \"postgres\",\n", + " \"password\": \"12345\",\n", + " \"host\": \"localhost\",\n", + " \"port\": \"5432\"\n", + "}\n", + "\n", + "# Veritabanı bağlantı bilgileri\n", + "credentials = {\n", + " \"dbname\": \"company\",\n", + " \"user\": \"postgres\",\n", + " \"password\": \"12345\",\n", + " \"host\": \"localhost\",\n", + " \"port\": \"5432\"\n", + "}\n", + "\n", + "# 1. Veritabanını oluştur (yoksa)\n", + "def create_database(creds):\n", + " conn = psycopg2.connect(\n", + " dbname=\"postgres\", # PostgreSQL'in varsayılan veritabanı\n", + " user=creds[\"user\"],\n", + " password=creds[\"password\"],\n", + " host=creds[\"host\"],\n", + " port=creds[\"port\"]\n", + " )\n", + " conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)\n", + " cur = conn.cursor()\n", + " try:\n", + " cur.execute(f\"CREATE DATABASE {creds['dbname']};\")\n", + " print(f\"{creds['dbname']} veritabanı oluşturuldu.\")\n", + " except psycopg2.errors.DuplicateDatabase:\n", + " print(f\"{creds['dbname']} veritabanı zaten mevcut.\")\n", + " finally:\n", + " cur.close()\n", + " conn.close()\n", + "\n", + "# 2. Tabloları oluştur ve veri ekle\n", + "def create_tables_and_insert_data(creds):\n", + " conn = psycopg2.connect(**creds)\n", + " cur = conn.cursor()\n", + "\n", + " # Mevcutsa silip yeniden oluşturmak istiyorsan önce drop komutu da eklenebilir:\n", + " cur.execute(\"DROP TABLE IF EXISTS departments;\")\n", + " cur.execute(\"DROP TABLE IF EXISTS employees;\")\n", + "\n", + " # departments tablosu\n", + " cur.execute(\"\"\"\n", + " CREATE TABLE departments (\n", + " emp_id INTEGER PRIMARY KEY,\n", + " dept_name VARCHAR(50),\n", + " dept_id INTEGER\n", + " );\n", + " \"\"\")\n", + "\n", + " # employees tablosu\n", + " cur.execute(\"\"\"\n", + " CREATE TABLE employees (\n", + " emp_id INTEGER PRIMARY KEY,\n", + " first_name VARCHAR(50),\n", + " last_name VARCHAR(50),\n", + " salary INTEGER,\n", + " job_title VARCHAR(100),\n", + " gender VARCHAR(10),\n", + " hire_date DATE\n", + " );\n", + " \"\"\")\n", + "\n", + " # Örnek veriler\n", + " departments_data = [\n", + " (17679, \"Operations\", 13),\n", + " (26650, \"Marketing\", 14),\n", + " (30840, \"Operations\", 13),\n", + " (49823, \"Technology\", 12),\n", + " (51821, \"Operations\", 13),\n", + " (67323, \"Marketing\", 14),\n", + " (71119, \"Administrative\", 11),\n", + " (76589, \"Operations\", 13),\n", + " (97927, \"Technology\", 12)\n", + " ]\n", + "\n", + " employees_data = [\n", + " (17679, \"Robert\", \"Gilmore\", 110000, \"Operations Director\", \"Male\", \"2018-09-04\"),\n", + " (26650, \"Elvis\", \"Ritter\", 86000, \"Sales Manager\", \"Male\", \"2017-11-24\"),\n", + " (30840, \"David\", \"Barrow\", 85000, \"Data Scientist\", \"Male\", \"2019-12-02\"),\n", + " (49714, \"Hugo\", \"Forester\", 55000, \"IT Support Specialist\", \"Male\", \"2019-11-22\"),\n", + " (51821, \"Linda\", \"Foster\", 95000, \"Data Scientist\", \"Female\", \"2019-04-29\"),\n", + " (67323, \"Lisa\", \"Wiener\", 75000, \"Business Analyst\", \"Female\", \"2018-08-09\"),\n", + " (70950, \"Rodney\", \"Weaver\", 87000, \"Project Manager\", \"Male\", \"2018-12-20\"),\n", + " (71329, \"Gayle\", \"Meyer\", 77000, \"HR Manager\", \"Female\", \"2019-06-28\"),\n", + " (76589, \"Jason\", \"Christian\", 99000, \"Project Manager\", \"Male\", \"2019-01-21\"),\n", + " (97927, \"Billie\", \"Lanning\", 67000, \"Web Developer\", \"Female\", \"2018-06-25\")\n", + " ]\n", + "\n", + " # Verileri ekle\n", + " cur.executemany(\"\"\"\n", + " INSERT INTO departments (emp_id, dept_name, dept_id)\n", + " VALUES (%s, %s, %s);\n", + " \"\"\", departments_data)\n", + "\n", + " cur.executemany(\"\"\"\n", + " INSERT INTO employees (emp_id, first_name, last_name, salary, job_title, gender, hire_date)\n", + " VALUES (%s, %s, %s, %s, %s, %s, %s);\n", + " \"\"\", employees_data)\n", + "\n", + " conn.commit()\n", + " cur.close()\n", + " conn.close()\n", + " print(\"Tablolar oluşturuldu ve veriler eklendi.\")\n", + "\n", + "# Hepsini çalıştır\n", + "create_database(credentials)\n", + "create_tables_and_insert_data(credentials)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "782347a8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "company veritabanı zaten mevcut.\n" + ] + } + ], + "source": [ + "def create_database(creds):\n", + " conn = psycopg2.connect(\n", + " dbname=\"postgres\",\n", + " user=creds[\"user\"],\n", + " password=creds[\"password\"],\n", + " host=creds[\"host\"],\n", + " port=creds[\"port\"]\n", + " )\n", + " conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)\n", + " cur = conn.cursor()\n", + " try:\n", + " cur.execute(f\"CREATE DATABASE {creds['dbname']};\")\n", + " print(f\"{creds['dbname']} veritabanı oluşturuldu.\")\n", + " except psycopg2.errors.DuplicateDatabase:\n", + " print(f\"{creds['dbname']} veritabanı zaten mevcut.\")\n", + " finally:\n", + " cur.close()\n", + " conn.close()\n", + "\n", + "create_database(credentials)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "185907c2", + "metadata": {}, + "outputs": [], + "source": [ + "def run_query(query, creds):\n", + " conn = psycopg2.connect(**creds)\n", + " cur = conn.cursor()\n", + " cur.execute(query)\n", + " results = cur.fetchall()\n", + " for row in results:\n", + " print(row)\n", + " cur.close()\n", + " conn.close()" + ] + }, + { + "cell_type": "markdown", + "id": "98eef3aa", + "metadata": {}, + "source": [ + "1 - Find the employees who get paid more than Rodney Weaver." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2f3ccbad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore', 110000)\n", + "('Linda', 'Foster', 95000)\n", + "('Jason', 'Christian', 99000)\n" + ] + } + ], + "source": [ + "query = \"\"\"\n", + "SELECT first_name, last_name, salary\n", + "FROM employees\n", + "WHERE salary > (\n", + " SELECT salary FROM employees\n", + " WHERE first_name = 'Rodney' AND last_name = 'Weaver'\n", + ");\n", + "\"\"\"\n", + "\n", + "run_query(query, credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "0cc3d918", + "metadata": {}, + "source": [ + "2 - Find the average, min and max salaries" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "eb7d3b7a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(Decimal('83600.000000000000'), 55000, 110000)\n" + ] + } + ], + "source": [ + "query2 = \"\"\"\n", + "SELECT AVG(salary) AS avg_salary,\n", + " MIN(salary) AS min_salary,\n", + " MAX(salary) AS max_salary\n", + "FROM employees;\n", + "\"\"\"\n", + "run_query(query2, credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "441acbe5", + "metadata": {}, + "source": [ + "3 - Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ad5339d6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore', 110000)\n", + "('Elvis', 'Ritter', 86000)\n", + "('David', 'Barrow', 85000)\n", + "('Hugo', 'Forester', 55000)\n", + "('Linda', 'Foster', 95000)\n", + "('Lisa', 'Wiener', 75000)\n", + "('Rodney', 'Weaver', 87000)\n", + "('Gayle', 'Meyer', 77000)\n", + "('Jason', 'Christian', 99000)\n", + "('Billie', 'Lanning', 67000)\n" + ] + } + ], + "source": [ + "query3 = \"\"\"\n", + "SELECT first_name, last_name, salary\n", + "FROM employees\n", + "WHERE salary > 8700;\n", + "\"\"\"\n", + "run_query(query3, credentials)\n" + ] + }, + { + "cell_type": "markdown", + "id": "1185cde6", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b915a1f3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Robert', 'Gilmore')\n", + "('David', 'Barrow')\n", + "('Linda', 'Foster')\n", + "('Jason', 'Christian')\n" + ] + } + ], + "source": [ + "query4 = \"\"\"\n", + "SELECT e.first_name, e.last_name\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE d.dept_name = 'Operations';\n", + "\"\"\"\n", + "run_query(query4, credentials)\n", + "#31354422222222222" + ] + }, + { + "cell_type": "markdown", + "id": "68db64e1", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c520fe98", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Billie', 'Lanning')\n" + ] + } + ], + "source": [ + "query5 = \"\"\"\n", + "SELECT e.first_name, e.last_name\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE d.dept_name = 'Technology';\n", + "\"\"\"\n", + "run_query(query5, credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "69cbf637", + "metadata": {}, + "source": [ + "6 - Find the average salary of female employees." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "d063513c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(Decimal('78500.000000000000'),)\n" + ] + } + ], + "source": [ + "query6 = \"\"\"\n", + "SELECT AVG(salary) AS avg_female_salary\n", + "FROM employees\n", + "WHERE gender = 'Female';\n", + "\"\"\"\n", + "run_query(query6, credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "2daebe7c", + "metadata": {}, + "source": [ + "7 - Find the average salaries of each department." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "fd6b9a41", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Technology', Decimal('67000.000000000000'))\n", + "('Marketing', Decimal('80500.000000000000'))\n", + "('Operations', Decimal('97250.000000000000'))\n" + ] + } + ], + "source": [ + "query7 = \"\"\"\n", + "SELECT d.dept_name, AVG(e.salary) AS avg_salary\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "GROUP BY d.dept_name;\n", + "\"\"\"\n", + "run_query(query7, credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "56d2045d", + "metadata": {}, + "source": [ + "8 - Find the oldest and newest employees." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "dda1a998", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Elvis', 'Ritter', datetime.date(2017, 11, 24))\n", + "('David', 'Barrow', datetime.date(2019, 12, 2))\n" + ] + } + ], + "source": [ + "query8 = \"\"\"\n", + "SELECT first_name, last_name, hire_date\n", + "FROM employees\n", + "WHERE hire_date = (SELECT MIN(hire_date) FROM employees)\n", + " OR hire_date = (SELECT MAX(hire_date) FROM employees);\n", + "\"\"\"\n", + "run_query(query8, credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "95de0e03", + "metadata": {}, + "source": [ + "9 - Find the hiring date and department of the highest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "fea4e9aa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(datetime.date(2018, 9, 4), 'Operations')\n" + ] + } + ], + "source": [ + "query9 = \"\"\"\n", + "SELECT e.hire_date, d.dept_name\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE e.salary = (SELECT MAX(salary) FROM employees);\n", + "\"\"\"\n", + "run_query(query9, credentials)\n" + ] + }, + { + "cell_type": "markdown", + "id": "5f6a39a7", + "metadata": {}, + "source": [ + "10 - Find the hiring date and department of the lowest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "24704580", + "metadata": {}, + "outputs": [], + "source": [ + "query10 = \"\"\"\n", + "SELECT e.hire_date, d.dept_name\n", + "FROM employees e\n", + "JOIN departments d ON e.emp_id = d.emp_id\n", + "WHERE e.salary = (SELECT MIN(salary) FROM employees);\n", + "\"\"\"\n", + "run_query(query10, credentials)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Mustafa.ipynb b/Mustafa.ipynb new file mode 100644 index 0000000..d7a23ba --- /dev/null +++ b/Mustafa.ipynb @@ -0,0 +1,569 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ccfde88c", + "metadata": {}, + "source": [ + "Database ve tabloların oluşturulup kayıtların eklenmesi" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "603feaef", + "metadata": {}, + "outputs": [], + "source": [ + "import psycopg2\n", + "\n", + "# Global olarak erişilecek nesneler\n", + "conn = None\n", + "cur = None\n", + "\n", + "def baslat():\n", + " global conn, cur\n", + " conn = psycopg2.connect(\n", + " host=\"localhost\",\n", + " dbname=\"homework\",\n", + " user=\"postgres\",\n", + " password=\"1234\",\n", + " port=\"5432\"\n", + " )\n", + " cur = conn.cursor()\n", + " print(\"Veritabanı bağlantısı açıldı.\")\n", + "\n", + "def bitir():\n", + " global conn, cur\n", + " if cur:\n", + " cur.close()\n", + " if conn:\n", + " conn.commit()\n", + " conn.close()\n", + " print(\"Veritabanı bağlantısı kapatıldı.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5facdffc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "Tablolar oluşturuldu (varsa zaten dokunulmadı).\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"\n", + " CREATE TABLE IF NOT EXISTS employees (\n", + " emp_id INT PRIMARY KEY,\n", + " first_name VARCHAR(50),\n", + " last_name VARCHAR(50),\n", + " salary INT,\n", + " job_title VARCHAR(55),\n", + " gender VARCHAR(6) CHECK (gender IN ('Female', 'Male')),\n", + " hire_date DATE\n", + " );\n", + "\n", + " CREATE TABLE IF NOT EXISTS departments (\n", + " dept_id INT,\n", + " dept_name VARCHAR(50),\n", + " emp_id INT REFERENCES employees(emp_id)\n", + " );\n", + "\"\"\")\n", + "\n", + "print(\"Tablolar oluşturuldu (varsa zaten dokunulmadı).\")\n", + "\n", + "bitir()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "a23cc73f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "cur.execute(\"\"\"\n", + " INSERT INTO employees (emp_id, first_name, last_name, salary, job_title, gender, hire_date) VALUES\n", + " (17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', '2018-09-04'),\n", + " (26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', '2017-11-24'),\n", + " (30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', '2019-12-02'),\n", + " (49714, 'Hugo', 'Forester', 55000, 'IT Support Specialist', 'Male', '2019-11-22'),\n", + " (51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', '2019-04-29'),\n", + " (67323, 'Lisa', 'Wiener', 75000, 'Business Analyst', 'Female', '2018-08-09'),\n", + " (70950, 'Rodney', 'Weaver', 87000, 'Project Manager', 'Male', '2018-12-20'),\n", + " (71329, 'Gayle', 'Meyer', 77000, 'HR Manager', 'Female', '2019-06-28'),\n", + " (76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', '2019-01-21'),\n", + " (97927, 'Bille', 'Lanning', 67000, 'Web Developer', 'Female', '2018-06-25');\n", + "\"\"\")\n", + "\n", + "cur.execute(\"\"\"\n", + " INSERT INTO departments (dept_id, dept_name, emp_id) VALUES\n", + " (13, 'Operations', 17679),\n", + " (14, 'Marketing', 26650),\n", + " (13, 'Operations', 30840),\n", + " (12, 'Technology', 49714),\n", + " (13, 'Operations', 51821),\n", + " (14, 'Marketing', 67323),\n", + " (11, 'Administrative', 70950),\n", + " (13, 'Operations', 76589),\n", + " (12, 'Technology', 97927);\n", + "\"\"\")\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "98eef3aa", + "metadata": {}, + "source": [ + "1 - Find the employees who get paid more than Rodney Weaver." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2f3ccbad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "(17679, 'Robert', 'Gilmore', 110000, 'Operations Director', 'Male', datetime.date(2018, 9, 4))\n", + "(51821, 'Linda', 'Foster', 95000, 'Data Scientist', 'Female', datetime.date(2019, 4, 29))\n", + "(76589, 'Jason', 'Christian', 99000, 'Project Manager', 'Male', datetime.date(2019, 1, 21))\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT * FROM employees \n", + " WHERE salary > (SELECT salary FROM employees WHERE first_name = 'Rodney' AND last_name = 'Weaver');\n", + "\"\"\")\n", + "\n", + "for row in cur.fetchall():\n", + " print(row)\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "0cc3d918", + "metadata": {}, + "source": [ + "2 - Find the average, min and max salaries" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "eb7d3b7a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "ortalama maaş: 83600.00\n", + "maximum maaş: 110000\n", + "minimum maaş: 55000\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"\n", + " SELECT \n", + " ROUND(AVG(salary), 2) AS average_salary,\n", + " MAX(salary) AS max_salary,\n", + " MIN(salary) AS min_salary\n", + " FROM employees;\n", + "\"\"\")\n", + "result = cur.fetchall()\n", + "print(f\"ortalama maaş: {result[0][0]}\")\n", + "print(f\"maximum maaş: {result[0][1]}\")\n", + "print(f\"minimum maaş: {result[0][2]}\")\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "441acbe5", + "metadata": {}, + "source": [ + "3 - Find the employees whose salary is more than 8700. Our query should return first name, last name, and salary info of the employees." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ad5339d6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "Robert Gilmore - 110000\n", + "Linda Foster - 95000\n", + "Jason Christian - 99000\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT first_name, last_name, salary FROM employees\n", + "WHERE salary > 87000\"\"\")\n", + "result = cur.fetchall()\n", + "for a,b,c in result:\n", + " print(a,b,\"-\",c)\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "1185cde6", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b915a1f3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "Robert Gilmore\n", + "Elvis Ritter\n", + "David Barrow\n", + "Hugo Forester\n", + "Linda Foster\n", + "Lisa Wiener\n", + "Rodney Weaver\n", + "Jason Christian\n", + "Bille Lanning\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT first_name, last_name\n", + "FROM departments JOIN employees\n", + "ON departments.emp_id = employees.emp_id;\"\"\")\n", + "for a,b in cur.fetchall():\n", + " print(a,b)" + ] + }, + { + "cell_type": "markdown", + "id": "68db64e1", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c520fe98", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "Hugo Forester\n", + "Bille Lanning\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT first_name, last_name\n", + "FROM departments JOIN employees\n", + "ON departments.emp_id = employees.emp_id\n", + "WHERE departments.dept_name = 'Technology';\"\"\")\n", + "\n", + "for a,b in cur.fetchall():\n", + " print(a,b)" + ] + }, + { + "cell_type": "markdown", + "id": "69cbf637", + "metadata": {}, + "source": [ + "6 - Find the average salary of female employees." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "d063513c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "78500.00\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT ROUND(AVG(salary),2)\n", + "FROM employees\n", + "WHERE gender = 'Female';\"\"\")\n", + "\n", + "ortalama = cur.fetchone()\n", + "print(ortalama[0])\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "2daebe7c", + "metadata": {}, + "source": [ + "7 - Find the average salaries of each department." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "fd6b9a41", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "Departman Adı - Ortalama Maaş\n", + "------------------------------\n", + "Technology - 61000.00\n", + "Marketing - 80500.00\n", + "Operations - 97250.00\n", + "Administrative - 87000.00\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT departments.dept_name AS \"Departman Adı\", ROUND(AVG(salary),2) AS \"Ortalama_maaş\"\n", + "FROM employees JOIN departments\n", + "ON employees.emp_id = departments.emp_id \n", + "GROUP BY departments.dept_name;\"\"\")\n", + "\n", + "rows = cur.fetchall()\n", + "\n", + "print(\"Departman Adı - Ortalama Maaş\")\n", + "print(\"------------------------------\")\n", + "\n", + "for row in rows:\n", + " print(f\"{row[0]} - {row[1]}\")\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "56d2045d", + "metadata": {}, + "source": [ + "8 - Find the oldest and newest employees." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "dda1a998", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "En uzun Çalışan:\n", + "(26650, 'Elvis', 'Ritter', 86000, 'Sales Manager', 'Male', datetime.date(2017, 11, 24))\n", + "En kısa Çalışan:\n", + "(30840, 'David', 'Barrow', 85000, 'Data Scientist', 'Male', datetime.date(2019, 12, 2))\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT * FROM employees\n", + "ORDER BY hire_date ASC\n", + "LIMIT 1;\"\"\")\n", + "\n", + "print(f\"En uzun Çalışan:\\n{cur.fetchone()}\")\n", + "\n", + "cur.execute(\"\"\"SELECT * FROM employees\n", + "ORDER BY hire_date DESC\n", + "LIMIT 1;\"\"\")\n", + "\n", + "print(f\"En kısa Çalışan:\\n{cur.fetchone()}\")\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "95de0e03", + "metadata": {}, + "source": [ + "9 - Find the hiring date and department of the highest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "fea4e9aa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "En yüksek maaş alanın işe giriş tarihi: 2019-11-22\n", + "En yüksek maaş alanın departman adı: Technology\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT hire_date, dept_name\n", + "FROM employees JOIN departments\n", + "ON employees.emp_id = departments.emp_id\n", + "ORDER BY salary\n", + "LIMIT 1;\"\"\")\n", + "\n", + "result = cur.fetchall()\n", + "\n", + "print(f\"En yüksek maaş alanın işe giriş tarihi: {result[0][0]}\")\n", + "print(f\"En yüksek maaş alanın departman adı: {result[0][1]}\")\n", + "\n", + "bitir()" + ] + }, + { + "cell_type": "markdown", + "id": "5f6a39a7", + "metadata": {}, + "source": [ + "10 - Find the hiring date and department of the lowest paid employee" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "24704580", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Veritabanı bağlantısı açıldı.\n", + "En düşük maaş alanın işe giriş tarihi: 2018-09-04\n", + "En düşük maaş alanın departman adı: Operations\n", + "Veritabanı bağlantısı kapatıldı.\n" + ] + } + ], + "source": [ + "baslat()\n", + "\n", + "cur.execute(\"\"\"SELECT hire_date, dept_name\n", + "FROM employees JOIN departments\n", + "ON employees.emp_id = departments.emp_id\n", + "ORDER BY salary DESC\n", + "LIMIT 1;\"\"\")\n", + "\n", + "result = cur.fetchall()\n", + "\n", + "print(f\"En düşük maaş alanın işe giriş tarihi: {result[0][0]}\")\n", + "print(f\"En düşük maaş alanın departman adı: {result[0][1]}\")\n", + "\n", + "bitir()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}