MySQL Tutorial
MySQL Tutorial for beginners to learn MySQL online for free.
MySQL is the most popular SQL based database management system. It is an open-source database distributed by Oracle Corporation. MySQL is mostly used with PHP to create dynamic web applications.
This Tutorial covers basic database queries such as SELECT, INSERT, UPDATE, DELETE along with Joins and subqueries.
In this online MySQL training course, we include database operations using easy to understand queries and examples. You will also find exercise, codes, Interview Questions to enhance your knowledge at the end of the most chapters.
MySQL Introduction
MySQL is a free and very popular relational database system. A database is a place where a website or app stores its data in an organised way, like users, products, orders and…
MySQL Installation and Setup
You need a MySQL server on your computer to practise. There are two easy ways. Option 1: XAMPP (easiest for beginners) Download XAMPP from apachefriends.org and install it. Open the XAMPP Control…
MySQL Data Types
Every column in a table has a data type. It tells MySQL what kind of value the column can store, and how much space to use. Numeric types Type Use Example INT…
MySQL CREATE DATABASE
A database is the container for your tables. The CREATE DATABASE statement makes a new one. IF NOT EXISTS If the database already exists, the command above gives an error. Add IF…
MySQL USE and SHOW DATABASES
A MySQL server can hold many databases. Before you work with tables, you must tell MySQL which database to use. SHOW DATABASES The databases information_schema, mysql, performance_schema and sys belong to MySQL…
MySQL DROP DATABASE
DROP DATABASE deletes a database, and all the tables and data inside it. The number of rows affected shows how many tables were removed. IF EXISTS If the database does not exist,…
MySQL CREATE TABLE
A table stores your data in rows and columns. CREATE TABLE defines the table name and its columns. id INT AUTO_INCREMENT PRIMARY KEY: a unique number that MySQL fills automatically. name VARCHAR(50)…
MySQL Constraints
Constraints are rules for the data in a column. MySQL refuses any data that breaks the rules, so your data stays correct. Constraint Rule PRIMARY KEY Unique and not empty. Identifies each…
MySQL INSERT INTO
INSERT INTO adds new rows to a table. After the table name we list the columns. VALUES has the values in the same order. We did not give id, so AUTO_INCREMENT made…
MySQL SELECT Statement
SELECT reads data from a table. It is the command you will use most. All columns * means all columns. Specific columns Calculated columns You can calculate values in a query. Give…
MySQL WHERE Clause
The WHERE clause filters the rows. Only the rows where the condition is true are returned. Text values are written in single quotes. Numbers are not. Comparison operators Operator Meaning = Equal…
MySQL AND, OR and NOT
To use more than one condition in WHERE, join them with AND, OR and NOT. Operator Row is returned when AND Both conditions are true OR At least one condition is true…
MySQL ORDER BY
ORDER BY sorts the result. The default is ascending (ASC): A to Z and small to big. Use DESC for the opposite. Sort by more than one column If two rows have…
MySQL LIMIT
LIMIT restricts how many rows are returned. MySQL does not have TOP like other databases, so we use LIMIT with ORDER BY. Read the interview question How to select Top 10 records…
MySQL DISTINCT
A column often has the same value in many rows. SELECT DISTINCT returns each value only once. Count unique values DISTINCT with more columns With more than one column, DISTINCT looks at…
MySQL LIKE and Wildcards
LIKE searches for a pattern in text. Two wildcards make the pattern. Wildcard Meaning % Any number of characters (even none) _ Exactly one character Pattern Matches ‘A%’ Starts with A ‘%a’…
MySQL IN, BETWEEN and IS NULL
IN: match a list IN is a short way to write many OR conditions. BETWEEN: match a range BETWEEN includes both end values. Read Write a query to select students with marks…
MySQL UPDATE Query
UPDATE changes the data of existing rows. Update many columns Calculate the new value Both Mumbai students got 5 more marks. Put the data back The next chapters use the original data,…
MySQL DELETE Query
DELETE removes rows from a table. Put the row back DELETE, TRUNCATE and DROP Command What it removes Can use WHERE? DELETE FROM t WHERE … Some or all rows. Table stays…
MySQL DROP TABLE
DROP TABLE deletes a table completely: its structure and all of its data. IF EXISTS This does not give an error when the table is not there. MySQL only shows a warning….
MySQL ALTER TABLE
ALTER TABLE changes the structure of a table that already exists, without losing its data. Add a column Existing rows get NULL in the new column. You can choose the place with…
MySQL Aliases
An alias is a temporary name for a column or a table. It exists only while the query runs. You create it with AS. Column alias The headers of the result are…
MySQL Aggregate Functions
Aggregate functions calculate one value from many rows. Function Returns COUNT() Number of rows SUM() Total of a column AVG() Average of a column MIN() Smallest value MAX() Biggest value COUNT(*) and…
MySQL GROUP BY and HAVING
GROUP BY puts rows with the same value in a column into groups, and an aggregate function calculates one value for each group. There is one row for each city. Delhi has…
MySQL JOINs
A JOIN combines rows from two or more tables, using a column that connects them. Here students.course_id points to courses.id. The students table has the course_id values 1, 2, 1, 3, NULL,…
MySQL Subqueries
A subquery is a SELECT inside another query, written in brackets. The inner query runs first, and its result is used by the outer query. Subquery with a comparison Find the students…
MySQL UNION
UNION puts the results of two or more SELECT queries one below the other. Every query must have the same number of columns. The columns must have compatible data types, in the…
MySQL Indexes
An index is like the index at the back of a book. Without it, MySQL must read every row of a table to find a value (a full table scan). With an…
MySQL Views
A view is a saved SELECT query that behaves like a table. It stores no data of its own: each time you read the view, MySQL runs the query. You can use…
MySQL String Functions
MySQL has many functions to work with text. You can use them in SELECT, WHERE and UPDATE. Function Use CONCAT(a, b, …) Joins texts CONCAT_WS(sep, a, b) Joins with a separator UPPER(),…
MySQL Date Functions
Dates are stored in the format YYYY-MM-DD. These functions help to read and calculate dates. NOW() gives the current date and time, CURDATE() only the date, and CURTIME() only the time. The…
MySQL Transactions
A transaction is a group of SQL commands that are treated as one. Either all of them succeed, or none of them is saved. A classic example is moving money from one…