Create Table Insert Into Select Update Delete
Create Table Insert Into Select Update Delete
Learning SQL from the basics is a good starting point for understanding how
to work with databases. Let's cover some fundamental concepts to get you started.
1. Introduction to Databases:
A database is a structured collection of data organized for efficient storage,
retrieval, and manipulation.
Databases can be classified as relational databases (SQL databases) or non-
relational databases (NoSQL databases).
2. Relational Databases and Tables:
A relational database consists of one or more tables, each containing rows
and columns.
Each table represents a specific entity or concept, and each row in the table
represents an instance of that entity.
Columns define the attributes of the entity, while rows contain the actual data.
3. Basic SQL Commands:
CREATE TABLE: Used to create a new table in the database.
INSERT INTO: Used to add new rows of data into a table.
SELECT: Used to retrieve data from one or more tables.
UPDATE: Used to modify existing data in a table.
DELETE: Used to remove rows from a table.
4. SQL Constraints:
Constraints are rules applied to the columns of a table to ensure data
integrity.
Common constraints include PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE,
etc.
5. SQL Functions:
SQL provides various built-in functions like COUNT, SUM, AVG, MAX, MIN, etc., for
data manipulation.
6. SQL Joins:
Joins are used to combine data from multiple tables based on a related
column.
Common join types are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
7. SQL Filtering and Sorting:
You can use the WHERE clause to filter data based on specific conditions.
The ORDER BY clause allows you to sort the results based on one or more
columns.
8. SQL Grouping and Aggregation:
The GROUP BY clause is used to group rows based on one or more columns.
Aggregation functions like SUM, COUNT, AVG, etc., can then be used to perform
calculations on the grouped data.
9. SQL Subqueries:
Subqueries are queries nested within the main query and can be used to
retrieve data for complex conditions.
10. SQL Indexes:
Indexes improve query performance by providing faster data access.
They are created on columns that are frequently used in search conditions.
Sure! Let's go through some basic SQL queries with examples to help you
understand how they work.
1. Creating a Table:
sqlCopy code
CREATE TABLE employees ( emp_id INT PRIMARY KEY, emp_name VARCHAR ( 50 ) NOT NULL ,
emp_age INT , emp_department VARCHAR ( 100 ) );
2. Inserting Data into the Table:
sqlCopy code
INSERT INTO employees (emp_id, emp_name, emp_age, emp_department) VALUES ( 1 , 'John Doe' , 30 ,
'Sales' ), ( 2 , 'Jane Smith' , 25 , 'Marketing' ), ( 3 , 'Bob Johnson' , 40 , 'Finance' );
3. Retrieving Data from the Table:
sqlCopy code
SELECT * FROM employees;
4. Filtering Data with WHERE Clause:
sqlCopy code
SELECT emp_name, emp_age FROM employees WHERE emp_department = 'Sales' ;
5. Updating Data in the Table:
sqlCopy code
UPDATE employees SET emp_age = 35 WHERE emp_id = 1 ;
6. Deleting Data from the Table:
sqlCopy code
DELETE FROM employees WHERE emp_id = 3 ;
7. Using Aggregate Functions (SUM, COUNT, AVG, MAX, MIN):
sqlCopy code
SELECT COUNT ( * ) AS total_employees FROM employees; SELECT MAX (emp_age) AS max_age
FROM employees; SELECT AVG (emp_age) AS avg_age FROM employees WHERE emp_department =
'Sales' ;
8. Joining Two Tables: Suppose we have another table called "departments" with
columns "dept_id" and "dept_name":
sqlCopy code
CREATE TABLE departments ( dept_id INT PRIMARY KEY, dept_name VARCHAR ( 100 ) NOT NULL
);
sqlCopy code
INSERT INTO departments (dept_id, dept_name) VALUES ( 1 , 'Sales' ), ( 2 , 'Marketing' ), ( 3 , 'Finance' );
Now, we can perform a join between the "employees" and "departments" tables:
sqlCopy code
SELECT emp_name, emp_age, dept_name FROM employees JOIN departments ON
employees.emp_department = departments.dept_name;
9. Using GROUP BY and Aggregate Functions Together:
sqlCopy code
SELECT emp_department, AVG (emp_age) AS avg_age FROM employees GROUP BY emp_department;
10. Using Subqueries:
sqlCopy code
SELECT emp_name, emp_age FROM employees WHERE emp_age > ( SELECT AVG (emp_age) FROM
employees);
These examples cover some basic SQL operations, but SQL can get much more
complex depending on your data and requirements. As you practice and encounter
more scenarios, you'll become more comfortable with SQL queries and their
applications. Feel free to experiment with these queries in a database to see the
results firsthand.