Help with SQL commands to interact with a MySQL database
- Open Source: MySQL is open-source and free
- Scalability: MySQL is ideal for both small and large applications
- Performance: MySQL is very fast, reliable, scalable, and easy to use
- Cross-Platform: MySQL is platform-independent, and it can run on various operating systems such as Windows, Linux, macOS, and others.
- Relational Database Management System (RDBMS): MySQL is based on a relational model, organizing data into tables with rows and columns.
- Huge websites like Facebook, Twitter, Airbnb, Booking.com, Uber, GitHub, YouTube, etc.
- Content Management Systems like WordPress, Drupal, Joomla!, Contao, etc.
- A very large number of web developers around the world
- RDBMS stands for Relational Database Management System.
- RDBMS is a program used to maintain a relational database.
- RDBMS uses SQL queries to access the data in the database.
- RDBMS is the basis for all modern database systems such as MySQL, Microsoft SQL Server, Oracle, and Microsoft Access.
- A table is a collection of related data entries, and it consists of columns and rows.
- A column holds specific information about every record in the table.
- A record (or row) is each individual entry that exists in a table.
- SQL is the standard language for dealing with Relational Databases.
- SQL is used to insert, search, update, and delete database records.
- SQL keywords are NOT case sensitive: select is the same as SELECT
- It is better all SQL keywords in upper-case.
- Some database systems require a semicolon at the end of each SQL statement.
- Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
- SELECT - extracts data from a database
- UPDATE - updates data in a database
- DELETE - deletes data from a database
- INSERT INTO - inserts new data into a database
- CREATE DATABASE - creates a new database
- ALTER DATABASE - modifies a database
- CREATE TABLE - creates a new table
- ALTER TABLE - modifies a table
- DROP TABLE - deletes a table
- CREATE INDEX - creates an index (search key)
- DROP INDEX - deletes an index
- CREATE TABLE
- ALTER TABLE
- DROP TABLE
- TRUNCATE TABLE
- INSERT
- UPDATE
- DELETE
- SELECT
- Mac /usr/local/mysql/bin
- Windows /Program Files/MySQL/MySQL version/bin
SHOW DATABASESCREATE DATABASE acme;DROP DATABASE acme;USE acme;CREATE TABLE users;CREATE TABLE IF NOT EXISTSCREATE TABLE users(
id INT AUTO_INCREMENT,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(50),
password VARCHAR(20),
location VARCHAR(100),
dept VARCHAR(100),
is_admin TINYINT(1),
register_date DATETIME,
PRIMARY KEY(id)
);CREATE TABLE example_table (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
varchar_column VARCHAR(255),
int_column INT,
float_column FLOAT,
double_column DOUBLE,
boolean_column BOOLEAN,
date_column DATE,
datetime_column DATETIME,
timestamp_column TIMESTAMP,
char_column CHAR(10),
text_column TEXT,
enum_column ENUM('Option1', 'Option2', 'Option3'),
set_column SET('Value1', 'Value2', 'Value3')
);
DROP TABLE tablename;DROP TABLE IF EXISTS tablename;SHOW TABLES;SELECT * FROM users;
SELECT first_name, last_name FROM users;SELECT * FROM users WHERE location = 'Massachusetts';
SELECT * FROM users WHERE location = 'Massachusetts' AND dept = 'sales';
SELECT * FROM users WHERE is_admin = 1;
SELECT * FROM users WHERE is_admin > 0;
-- Using `!=`
SELECT * FROM users WHERE location != 'Massachusetts';
-- Using `<>`
SELECT * FROM users WHERE location <> 'Massachusetts';
SELECT * FROM users WHERE location LIKE 'Mas%';
SELECT * FROM users WHERE location IN ('Massachusetts', 'New York', 'California');
SELECT * FROM users WHERE is_admin BETWEEN 1 AND 5;
Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, etc.!
Operators in The WHERE Clause The following operators can be used in the WHERE clause:
- = Equal
- > Greater than
- < Less than
- >= Greater than or equal
- <= Less than or equal
- <> Not equal. Note: In some versions of SQL this operator may be written as !=
- BETWEEN Between a certain range
- LIKE Search for a pattern
- IN To specify multiple possible values for a column
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;SELECT * FROM users ORDER BY last_name ASC;
SELECT * FROM users ORDER BY last_name DESC;INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);INSERT INTO table_name
VALUES (value1, value2, value3, ...);INSERT INTO users (first_name, last_name, email, password, location, dept, is_admin, register_date) values ('Brad', 'Traversy', '[email protected]', '123456','Massachusetts', 'development', 1, now());INSERT INTO users (first_name, last_name, email, password, location, dept, is_admin, register_date) values ('Fred', 'Smith', '[email protected]', '123456', 'New York', 'design', 0, now()), ('Sara', 'Watson', '[email protected]', '123456', 'New York', 'design', 0, now()),('Will', 'Jackson', '[email protected]', '123456', 'Rhode Island', 'development', 1, now()),('Paula', 'Johnson', '[email protected]', '123456', 'Massachusetts', 'sales', 0, now()),('Tom', 'Spears', '[email protected]', '123456', 'Massachusetts', 'sales', 0, now());INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');SELECT column_names
FROM table_name
WHERE column_name IS NULL;SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!
DELETE FROM table_name WHERE condition;DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted!
-
The LIMIT clause is used to specify the number of records to return.
-
The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;SELECT * FROM Customers
LIMIT 3;ALTER TABLE users ADD col_name col_definition;ALTER TABLE users MODIFY col_name col_definition;ALTER TABLE users DROP col_name;ALTER TABLE users RENAME old_col to new_col;SELECT CONCAT(first_name, ' ', last_name) AS 'Name', dept FROM users;
SELECT DISTINCT location FROM users;
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
SELECT * FROM users WHERE age BETWEEN 20 AND 25;The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
SELECT * FROM users WHERE dept LIKE 'd%';
SELECT * FROM users WHERE dept LIKE 'dev%';
SELECT * FROM users WHERE dept LIKE '%t';
SELECT * FROM users WHERE dept LIKE '%e%';SELECT * FROM users WHERE dept NOT LIKE 'd%';SELECT * FROM users WHERE dept IN ('design', 'sales');Aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
An alias is created with the AS keyword.
Aliases can be useful when:
- There are more than one table involved in a query
- Functions are used in the query
- Column names are big or not very readable
- Two or more columns are combined together
SELECT CustomerName AS Customer, ContactName AS "Contact Person"
FROM Customers;SELECT CustomerName, CONCAT_WS(', ', Address, PostalCode, City, Country) AS Address
FROM Customers;CREATE INDEX LIndex On users(location);
DROP INDEX LIndex ON users;CREATE TABLE posts(
id INT AUTO_INCREMENT,
user_id INT,
title VARCHAR(100),
body TEXT,
publish_date DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);INSERT INTO posts(user_id, title, body) VALUES (1, 'Post One', 'This is post one'),(3, 'Post Two', 'This is post two'),(1, 'Post Three', 'This is post three'),(2, 'Post Four', 'This is post four'),(5, 'Post Five', 'This is post five'),(4, 'Post Six', 'This is post six'),(2, 'Post Seven', 'This is post seven'),(1, 'Post Eight', 'This is post eight'),(3, 'Post Nine', 'This is post none'),(4, 'Post Ten', 'This is post ten');SELECT
users.first_name,
users.last_name,
posts.title,
posts.publish_date
FROM users
INNER JOIN posts
ON users.id = posts.user_id
ORDER BY posts.title;CREATE TABLE comments(
id INT AUTO_INCREMENT,
post_id INT,
user_id INT,
body TEXT,
publish_date DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id),
FOREIGN KEY(user_id) references users(id),
FOREIGN KEY(post_id) references posts(id)
);INSERT INTO comments(post_id, user_id, body) VALUES (1, 3, 'This is comment one'),(2, 1, 'This is comment two'),(5, 3, 'This is comment three'),(2, 4, 'This is comment four'),(1, 2, 'This is comment five'),(3, 1, 'This is comment six'),(3, 2, 'This is comment six'),(5, 4, 'This is comment seven'),(2, 3, 'This is comment seven');SELECT
comments.body,
posts.title
FROM comments
LEFT JOIN posts ON posts.id = comments.post_id
ORDER BY posts.title;
SELECT
comments.body,
posts.title,
users.first_name,
users.last_name
FROM comments
INNER JOIN posts on posts.id = comments.post_id
INNER JOIN users on users.id = comments.user_id
ORDER BY posts.title;
SELECT COUNT(id) FROM users;
SELECT MAX(age) FROM users;
SELECT MIN(age) FROM users;
SELECT SUM(age) FROM users;
SELECT UCASE(first_name), LCASE(last_name) FROM users;
SELECT age, COUNT(age) FROM users GROUP BY age;
SELECT age, COUNT(age) FROM users WHERE age > 20 GROUP BY age;
SELECT age, COUNT(age) FROM users GROUP BY age HAVING count(age) >=2;
-- Select all:
SELECT * FROM Customers;/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;- 10 MySQL Database Interview Questions for Beginners and Intermediates
- 100 MySQL interview questions
- 15 Basic MySQL Interview Questions for Database Administrators
- 28 MySQL interview questions from JavaTPoint.com
- 40 Basic MySQL Interview Questions with Answers
- Top 50 MySQL Interview Questions & Answers from Career Guru
If you want to contact with me you can reach me at Linkedin.