DAY _ 7 : PostgreSQL Query Session.

✅ Assessment session for PostgreSQLQuery Session.

To Create a table and insert data in PostgreSQL, use the CREATE TAB;E statement to define the table structure and the INSERT INTO statement to add rows. Now Create our first DATABASE where Named as " Employee Database "

       CREATE DATABASE  Employee Database ;

And Table name as "Employees"

🔜 Create a Table :

CREATE TABLE  Employees(

Emp_id  SERIAL PRIMARY KEY,

First_Name  VARCHAR(50) NOT NULL,

Last_Name  VARCHAR(50) NOT NULL,

Email  VARCHAR(100) NOT NULL UNIQUE,

Dept  VARCHAR(50),

Salary  DECIMAL(10,2) DEFAULT 30000.00,

Hire_Date   DATE NOT NULL DEFAULT CURRENT_DATE

);


✅  This code snippet is from :

  • CREATE TABLE  Employees : Create table named "Employees".
  • Emp_id  SERIAL PRIMARY KEY : Create a column named "Emp_id" as unique,autoincrementing primary key.
  • First_Name VARCHAR(50) NOT NULL : Creates a column for the first name, allowing up to 50 characters.
  • Last_Name VARCHAR(50) NOT NULL : Creates a column for the last name, allowing up to 50 characters.
  • Email  VARCHAR(100) NOT NULL UNIQUE : Creates a  column name "Email", allowing up to 100 characters.
  • Dept  VARCHAR(50) : Creates a column name "Department" , allowing up to 50 characters.
  • Salary  DECIMAL(10,2) DEFAULT 30000.00 : Creates a column name "Salary" , allowing up to Decimal(10,2) which means a decimal number with a total precison of 10 digits, including two digits after the decimal point and Default value as 30000.00.
  • Hire_Date   DATE NOT NULL DEFAULT CURRENT_DATE : Creates a column name "Joining Date" , allowing up to Default and Current Date.


✅  Inserting Data into the table.

INSERT INTO Employees (Emp_id, first_Name, Last_Name, email, dept, salary, hire_date)     VALUES


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES  (1,'Jenny','Sullivan','jenny.sullivan@example.com','Engineering',55380.73,'2018-07-05');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (2,'Amber','Lyons','amber.lyons@example.com','Marketing',70340.1,'2016-07-04');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (3,'Amanda','Lyons','amanda.lyons@example.com','Sales',48756.67,'2016-09-12');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (4,'Lisa','Meyer','lisa.meyer@example.com','Operations',73696.29,'2016-11-14');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (5,'Mary','Smith','mary.smith@example.com','Marketing',50670.63,'2025-03-31');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (6,'Kevin','Watkins','kevin.watkins@example.com','IT',118666.24,'2019-12-05');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (7,'John','Mays','john.mays@example.com','IT',92065.38,'2018-07-07');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (8,'Melissa','Bush','melissa.bush@example.com','HR',72343.88,'2019-05-23');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (9,'Alex','Jackson','alex.jackson@example.com','Sales',119213.58,'2016-12-08');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (10,'Tyler','Greene','tyler.greene@example.com','Finance',49526.97,'2023-06-06');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (11,'Danielle','Garza','danielle.garza@example.com','Marketing',49947.23,'2020-01-19');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (12,'Timothy','Middleton','timothy.middleton@example.com','Finance',80334.64,'2022-07-22');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (13,'Destiny','Hughes','destiny.hughes@example.com','Operations',83342.54,'2018-06-10');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (14,'Leslie','Martinez','leslie.martinez@example.com','Finance',56307.09,'2016-03-25');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (15,'Tonya','Grant','tonya.grant@example.com','IT',82126.27,'2016-02-06');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (16,'Nicholas','Flores','nicholas.flores@example.com','Finance',106131.03,'2023-06-09');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (17,'Christine','Mills','christine.mills@example.com','Finance',43653.1,'2022-03-23');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (18,'Antonio','Fuller','antonio.fuller@example.com','HR',101263.6,'2018-01-29');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (19,'Jennifer','Mendoza','jennifer.mendoza@example.com','IT',95617.39,'2018-12-09');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (20,'John','Fitzgerald','john.fitzgerald@example.com','Marketing',89084.87,'2024-05-30');


INSERT INTO Employee(Emp_Id,First_Name,Last_Name,Email,Depatment,Salary,Hire_Date) VALUES (21,'Deborah','Singleton','deborah.singleton@example.com','Finance',41801.93,'2021-10-29');


✅ This code anippet is from :

  • INSERT INTO Employees : Specifies tabe  to insert data into.
  • (Emp_id, first_Name, Last_Name, email, dept, salary, hire_date)  : Specifies the column to insert data into.
  • VALUES(1, 'Raj', 'Sharma', 'raj.sharma@example.com', 'IT', 50000.00, '2020-01-15') : Inserts data into the specificed columns.

Here, We Complete Our Creating and Inserting of data into the Employees Table.

Now move our next task as check data availability in table using of  " * ".

SELECT  *  FROM Employees;

Output as :


✅ Explanation :
  1. CREATE TABLE  Defines the structureof the table, including column names, data types, and constraints like the primary key.
  2. INSERT INTO  Adds rows (records) to the table, providing values for each column specified in the VALUES clause.




Comments