Welcome to the SQL Tutorial! Today, we're going to create a simple Employee Management system using SQL. This project is designed for beginners and intermediates, so let's get started! š
SQL (Structured Query Language) is a programming language used to manage and manipulate databases. It allows us to create, read, update, and delete records in a database. š” Pro Tip: SQL is a must-know skill for any developer!
Let's create a new database called employee_db. We'll also create a table called employees to store our employee data.
CREATE DATABASE employee_db;
USE employee_db;
CREATE TABLE employees (
id INT PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(255),
department VARCHAR(50),
salary DECIMAL(10, 2)
);š Note: We've created a table with fields for id, first_name, last_name, email, department, and salary. The id field is our primary key, which uniquely identifies each employee.
Now that we have our table set up, let's insert some data into it.
INSERT INTO employees (id, first_name, last_name, email, department, salary)
VALUES (1, 'John', 'Doe', 'john.doe@example.com', 'IT', 60000);
INSERT INTO employees (id, first_name, last_name, email, department, salary)
VALUES (2, 'Jane', 'Smith', 'jane.smith@example.com', 'HR', 55000);š Note: We've inserted two employees with their respective details.
Now that we have data in our database, let's learn how to query it.
SELECT * FROM employees;This query will return all the data from the employees table.
We can also filter and sort our data based on specific conditions.
SELECT * FROM employees WHERE department = 'IT';
SELECT * FROM employees ORDER BY salary DESC;š Note: The first query will return only the IT department employees, and the second query will return all employees sorted by salary in descending order.
We can also update and delete data in our database.
UPDATE employees SET salary = 65000 WHERE id = 1;
DELETE FROM employees WHERE id = 2;š Note: The first query will increase John's salary to 65000, and the second query will delete Jane's record from the employees table.
What does SQL stand for?
What does the `id` field in our `employees` table represent?
That's it for today! We've learned how to create a database and table, insert data, query data, filter and sort data, and update and delete data. Keep practicing, and you'll become a SQL pro in no time! š
Stay tuned for more tutorials on CodeYourCraft!