This is an SQL tutorial for beginners. We will discuss what is SQL and what it is used for.
You can also check this tutorial in the following video:
1. Why to Learn SQL
SQL stands for Structured Query Language and is a standard language for accessing and manipulating databases. Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.
A database contains tables that are identified by a name (e.g. Person, Invoice). Tables contain records (rows).
2. Applications of SQL
SQL is used to retrieve and manipulate data in the database. It can be used to create new databased, tables, views, etc. We can also use SQL to write Stored Procedures as well. We can also use it to set permissions for tables, views, etc.
SQL keywords are NOT case-sensitive. So select is the same as SELECT. Note that some databases require a semi-colon (;) at the end of each SQL statement.
3. SQL Tutorial – Data Types
A SQL data type defines what type of value a column in the table can hold. There is a subtle difference in the list of data types based on the database server you are using. Below we will discuss the data types available in the MySQL database. There are three main data types: String, Numeric, and date-time.
3.1 String Data Type
|CHAR(size)||A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the column length in characters – can be from 0 to 255. Default is 1|
|VARCHAR(size)||A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum column length in characters – can be from 0 to 65535|
|BINARY(size)||Equal to CHAR, but stores binary byte strings. The size parameter specifies the column length in bytes. Default is 1|
|VARBINARY(size)||Equal to VARCHAR, but stores binary byte strings. The size parameter specifies the maximum column length in bytes.|
|TINYTEXT||Holds a string with a maximum length of 255 characters|
|TEXT(size)||Holds a string with a maximum length of 65,535 bytes|
|MEDIUMTEXT||Holds a string with a maximum length of 16,777,215 characters|
|LONGTEXT||Holds a string with a maximum length of 4,294,967,295 characters|
3.2 Numeric Data Type
|BIT(size)||A bit-value type. The number of bits per value is specified in size. The size parameter can hold a value from 1 to 64. The default value for size is 1.|
|TINYINT(size)||A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255. The size parameter specifies the maximum display width (which is 255)|
|SMALLINT(size)||A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to 65535. The size parameter specifies the maximum display width (which is 255)|
|MEDIUMINT(size)||A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from 0 to 16777215. The size parameter specifies the maximum display width (which is 255)|
|INT(size) / INTEGER(size)||A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to 4294967295. The size parameter specifies the maximum display width (which is 255)|
|BIGINT(size)||A large integer. Signed range is from -9223372036854775808 to 9223372036854775807. Unsigned range is from 0 to 18446744073709551615. The size parameter specifies the maximum display width (which is 255)|
|FLOAT(p)||A floating point number. MySQL uses the p value to determine whether to use FLOAT or DOUBLE for the resulting data type. If p is from 0 to 24, the data type becomes FLOAT. If p is from 25 to 53, the data type becomes DOUBLE|
|DOUBLE(size, d)||A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter|
|DECIMAL(size, d) / DEC(size, d)||An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value for size is 10. The default value for d is 0.|
3.3 Boolean Data Type
|BOOL/BOOLEAN||Zero is considered as false, nonzero values are considered as true.|
3.4 Large Objects
|TINYBLOB||For BLOBs (Binary Large Objects). Max length: 255 bytes|
|BLOB(size)||For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data|
|MEDIUMBLOB||For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data|
|LONGBLOB||For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data|
3.5 Date and Time Data Type
|DATE||A date. Format: YYYY-MM-DD. The supported range is from ‘1000-01-01’ to ‘9999-12-31’|
|DATETIME(fsp)||A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The supported range is from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. Adding DEFAULT and ON UPDATE in the column definition to get automatic initialization and updating to the current date and time|
|TIMESTAMP(fsp)||A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch (‘1970-01-01 00:00:00’ UTC). Format: YYYY-MM-DD hh:mm:ss. The supported range is from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC. Automatic initialization and updating to the current date and time can be specified using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP in the column definition|
|TIME(fsp)||A time. Format: hh:mm:ss. The supported range is from ‘-838:59:59’ to ‘838:59:59’|
|ENUM(val1, val2, val3, …)||A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are sorted in the order you enter them|
|SET(val1, val2, val3, …)||A string object that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values in a SET list|
4. SQL statements
4.1 SQL Select, From, Where
SELECT keyword is used to return the data from the table. A
FROM keyword is used to specify from where (table/views) the data needs to be fetched. A
WHERE keyword is used to apply filters before returning the data. It is used to extract only those records that fulfill a specified condition. Below are the list of operators which can be used in the
|>=||Greater than or equal|
|<=||Less than or equal|
|BETWEEN||Between a certain range|
|LIKE||Search for a pattern|
|IN||To specify multiple possible values for a column|
WHERE clause can be combined with the
NOT operators. We can use the
BY keyword to sort the result-set in ascending or descending order.
SELECT col1, col2 FROM table_name ORDER BY col1
The data returned is stored in a result table, called the result-set.
SELECT * FROM person WHERE first_name = 'Mike'
In the above query we are returning everything from the person table where the column first_name has a value ‘Mike’. The ‘*’ after the
SELECT statement is used to return all the columns for the given table. You can also specify the column name explicitly if you don’t want to return everything.
SELECT title,surname FROM person WHERE first_name = 'Mike'
The above query will only return the title and the surname column values. We can use the
DISTINCT keyword to filter out the duplicate values.
4.2 INSERT UPDATE DELETE
INSERT statement is used to insert data into the table.
INSERT INTO table (col1,col2) VALUES (val1,val2)
Make sure the order of the values is in the same order as the columns in the table. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query.
UPDATE statement is used to modify the existing records in a table.
UPDATE table_name SET col1 = val1, col2 = val2 WHERE conditions...
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!
The DELETE command is used to delete records in the table.
DELETE FROM table_name WHERE conditions...
UPDATE make sure you use the
WHERE condition otherwise it will delete all the records in that table.
A view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.You can add SQL statements and functions to a view and present the data as if the data were coming from one single table.
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
A view always shows up-to-date data! The database engine recreates the view, every time a user queries it. A view can be updated with the
CREATE OR REPLACE VIEW statement. A view is deleted with the
DROP VIEW statement
5. SQL Tutorial – Summary
That was an SQL tutorial for beginners. Firstly, we discussed what SQL is. Then, we discussed its usage and its applications. Finally, we discussed the various commands available and how to use them. We also looked at the available data types and when to use them.