Structured Query Language (SQL) is the standard language used to communicate with databases. It allows you to create, manipulate, and retrieve data efficiently. In this tutorial, we’ll explore essential SQL commands and concepts, helping you build a solid foundation for working with databases.
Queries that Create, drop, and modify database structures are DDL Queries
Queries used to manipulate the data are known as DML Queries.
Queries used to Retrieve data
Only DML statements such as INSERT, DELETE, and UPDATE can be used with TCL statements.
These functions cannot be used when creating or deleting tables because they are committed to the database.
DCL commands are used to grant and take back authority from any database user.
CREATE DATABASE database_name;
USE database_name;
CREATE TABLE table_name (
col1_name datatype,
col2_name datatype,
col3_name datatype,
...
);
SHOW DATABASES;
SHOW TABLES;
DESC table_name;
SELECT * FROM table_name;
INSERT INTO table_name (col1_name, col2_name, col3_name, ...)
VALUES (val1, val2, val3, ...);
INSERT INTO table_name (col1_name, col2_name, col3_name, ...)
VALUES
(val1, val2, val3, ...),
(val1, val2, val3, ...),
(val1, val2, val3, ...)
......................;
SELECT col1_name, col2_name, ... FROM table_name;
SELECT col1_name, col2_name, ... FROM table_name WHERE condition;
The CREATE command is used to create a new database, table, view, or other database objects.
Example : Creating a New table :
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
The DROP command is used to delete an existing database object, such as a table, database, or view. Be cautious when using DROP, as it permanently removes the object and all its data.
DROP TABLE table_name;
DROP DATABASE database_name;
The ALTER command is used to modify the structure of an existing database object, such as adding, deleting, or modifying columns in a table.
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name MODIFY COLUMN column_name datatype ;
The TRUNCATE command is used to remove all records from a table, but unlike DROP, it does not delete the table structure. It’s a fast way to delete all data from a table without removing the table itself.
Truncating a table :
TRUNCATE TABLE table_name;
The RENAME
command is used to rename an existing database object, such as a table. This command is useful when you want to change the name of an object without altering its structure or data.
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
--OR
ALTER TABLE table_name CHANGE old_name new_name datatype ;
RENAME TABLE old_name TO new_name;
The INSERT command is used to add new rows (records) to a table. You can insert a single row or multiple rows at once.
INSERT INTO table_name (col1_name , col2_name, col3_name,....) VALUES (val1 , val2, val3 , …….) ;
INSERT INTO table_name (col1_name , col2_name, col3_name,....)
VALUES (val1 , val2, val3 , …….) ,
(val1 , val2, val3 , …….),
(val1 , val2, val3 , …….) ,
(val1 , val2, val3 , …….),
.......................... ;
The UPDATE command is used to modify existing records in a table. You can update one or more columns for specific rows based on a condition.
UPDATE table_name SET column1 = val1 , column2 = val2 ,.... Where condition;
The DELETE command is used delete existing records in a table. You can delete one or more rows based on a condition.
DELETE FROM table_name WHERE condition ;
'='
Equal'>'
Greater than'<'
Less than'<='
Less than or equal to'>='
Greater than or equal to'<>'
Not equal to
SELECT function_name(col_name) FROM table_name WHERE condition ;
SELECT * FROM table_name WHERE col_name IN (val1, val2, val3, ...);
SELECT * FROM table_name WHERE col_name BETWEEN val1 AND val2;
Wildcard characters are used with Like operator. It is used to search for a specified pattern from a column.
SELECT * FROM table_name WHERE col_name BETWEEN val1 AND val2;
SELECT * FROM table_name WHERE condition ORDER BY col_name;
SELECT * FROM table_name WHERE condition ORDER BY col_name DESC;
It is used to return only distinct (different) values.
SELECT DISTINCT col_name FROM table_name WHERE condition;
It groups rows that have the same values into summary rows, like “find the number of employees in same department”. Group By is always used with aggregate functions by grouping the result-set by one or more columns.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
CREATE TABLE table_name (
col_name datatype UNIQUE,
another_col_name datatype NOT NULL DEFAULT default_value CHECK (condition)
);
CREATE TABLE table_name (
col_name datatype PRIMARY KEY
);
CREATE TABLE table_name (
col_name datatype,
FOREIGN KEY (col_name) REFERENCES another_table_name(another_col_name)
);
SELECT col1, col2 FROM table1 UNION SELECT col1, col2 FROM table2;
SELECT col1, col2 FROM table1 INTERSECT SELECT col1, col2 FROM table2;
Select col1,col2 ,.... From table1 UNION ALL select col1,col2,.... From table2;
Select col1,col2 ,.... From table1 EXCEPT select col1,col2,.... From table2;
SELECT col1, col2 FROM table1 INNER JOIN table2 ON table1.FK = table2.PK;
SELECT col1, col2 FROM table1 LEFT JOIN table2 ON table1.FK = table2.PK;
SELECT col1, col2 FROM table1 RIGHT JOIN table2 ON table1.FK = table2.PK;
SELECT col1, col2 FROM table1 CROSS JOIN table2;
In MySQL, the IF and CASE clauses are used to implement conditional logic within queries.
The IF clause in MySQL is used to perform conditional logic within a query.
IF(condition, true_value, false_value)
The CASE clause is more flexible and can handle multiple conditions.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
View is a virtual table i.e, it looks like a table but not an actual table as it does not acquire any physical space.
View is a result of a stored query.
CREATE VIEW view_name AS SELECT * FROM table_name WHERE condition;
View is like a table, thus table queries can be implemented on views except DDL queries.
SELECT * FROM view_name;
DROP VIEW view_name;
SHOW FULL TABLES IN database_name WHERE TABLE_TYPE LIKE 'VIEW';
The WITH CHECK OPTION clause is an optional clause that can be added to a CREATE VIEW statement to ensure that any updates or inserts to a view meet the view’s conditions. If the new row doesn’t meet the conditions, the database server will reject the operation with an error.
CREATE VIEW view_name AS select * from table_name where condition WITH CHECK OPTION;
Indexing in mysql is a tool that can improve the performance of databases by making it faster to retrieve data.
Index is also stored somewhere in the hard disk.
Index table consists of key value and pointer.
CREATE INDEX index_name ON table_name (column_name);
DROP INDEX index_name ON table_name;
SHOW INDEX FROM table_name;
It is a set of actions that run automatically when a specified change operation (Insert , Update or Delete) is performed on a specific table.
CREATE TRIGGER trigger_name BEFORE/AFTER
trigger_event ON
table_name FOR EACH ROW
BEGIN
...
END;
DROP TRIGGER trigger_name;
It is a named block of SQL statements that can be stored in the database and executed repeatedly whenever needed.
CREATE PROCEDURE procedure_name([parameter_list])
BEGIN
-- SQL statements;
END;
CALL procedure_name ;
Drop procedure procedure_name ;
MySQL’s robust functions make complex calculations and data manipulation simple for data managers.
It is a custom function created by the user to perform specific tasks.
to create a function you need to specify function name , parameters and its logic.
Create function function_name (parameter1_name datatype , parameter2_name datatype ,......)
Returns datatype
Begin
-- Queries;
Return result;
End;
You can call a function in your queries to perform the desired operation.
Select function_name(agruments);
Mastering SQL is essential for anyone working with databases, from beginners to seasoned developers. This tutorial has covered fundamental queries, operations, and advanced concepts to help you efficiently manage and manipulate data. With these skills, you’re well on your way to becoming proficient in database management.
If you’re looking to further enhance your SQL and gain hands-on experience, consider joining Netmax Technologies. As a premier institution in IT training, we offer comprehensive courses ranging from 45 days to 4-6 months, designed to prepare you for real-world challenges and career success. Our experienced instructors and practical training approach ensure that you gain the necessary knowledge and skills to thrive in the competitive field of powerbi. Enroll today and take the first step towards becoming a proficient data scientist with Netmax Technologies!