Practical 2: Insert Data
2024, May 8
The INSERT
statement is used to insert or add a row of data into a table. To insert records into a table, use the following syntax:
INSERT INTO tablename (first_column,...last_column)
VALUES (first_value,...last_value);
The column names must match the values you provide in order. Strings should be enclosed in single quotes, while numbers should not.
The following example shows how to insert a record into the employee
table:
INSERT INTO employee (first, last, age, address, city, state)
VALUES ('Luke', 'Duke', 45, '2130 Boars Nest', 'Hazard Co', 'Georgia');
Insert the following employees into your table:
INSERT INTO myemployees (firstname, lastname, title, age, salary)
VALUES ('Jonie', 'Weber', 'Secretary', 28, 19500.00);
INSERT INTO myemployees (firstname, lastname, title, age, salary)
VALUES ('Potsy', 'Weber', 'Programmer', 32, 45300.00);
INSERT INTO myemployees (firstname, lastname, title, age, salary)
VALUES ('Dirk', 'Smith', 'Programmer II', 45, 75020.00);
Add at least 5 more employees of your own choosing.
-- Example insert statements
INSERT INTO myemployees (firstname, lastname, title, age, salary)
VALUES ('Jenny', 'Smith', 'Programmer', 28, 45000.00),
('John', 'Doe', 'Manager', 35, 60000.00),
('Jane', 'Doe', 'Secretary', 25, 25000.00),
('Jack', 'Black', 'Programmer', 30, 50000.00),
('Jill', 'Hill', 'Programmer', 35, 55000.00);
After inserting data, use the following SELECT
statements to retrieve specific information:
SELECT * FROM myemployees;
SELECT * FROM myemployees
WHERE salary > 30000;
SELECT firstname, lastname FROM myemployees
WHERE age < 30;
SELECT firstname, lastname, salary FROM myemployees
WHERE title LIKE '%Programmer%';
SELECT * FROM myemployees
WHERE lastname LIKE '%ebe%';
SELECT firstname FROM myemployees
WHERE firstname = 'Potsy';
SELECT * FROM myemployees
WHERE age > 80;
SELECT * FROM myemployees
WHERE lastname LIKE '%ith';
Create at least 5 of your own SELECT
statements to retrieve specific information.
-- Example custom select statements
-- Select all columns for everyone whose first name starts with 'J'
SELECT * FROM myemployees
WHERE firstname LIKE 'J%';
-- Select the first name and salary for everyone whose salary is less than 40000
SELECT firstname, salary FROM myemployees
WHERE salary < 40000;
-- Select all columns for everyone who has 'Secretary' in their title
SELECT * FROM myemployees
WHERE title = 'Secretary';
-- Select the last name and age for everyone older than 40
SELECT lastname, age FROM myemployees
WHERE age > 40;
-- Select all columns for everyone whose city is 'Hazard Co'
SELECT * FROM myemployees
WHERE city = 'Hazard Co';