sql

SQL Tutorial for Beginners

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:

SQL Tutorial for Beginners – 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 tutorial

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

Data typeDescription
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.
TINYTEXTHolds a string with a maximum length of 255 characters
TEXT(size)Holds a string with a maximum length of 65,535 bytes
MEDIUMTEXTHolds a string with a maximum length of 16,777,215 characters
LONGTEXTHolds a string with a maximum length of 4,294,967,295 characters
SQL String Data Type

3.2 Numeric Data Type

Data typeDescription
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(sized)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(sized) / DEC(sized)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.
SQL Numeric Data Type

3.3 Boolean Data Type

Data typeDescription
BOOL/BOOLEANZero is considered as false, nonzero values are considered as true.
SQL Boolean Data Type

3.4 Large Objects

Data typeDescription
TINYBLOBFor BLOBs (Binary Large Objects). Max length: 255 bytes
BLOB(size)For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
MEDIUMBLOBFor BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
LONGBLOBFor BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data
SQL Large objects data types

3.5 Date and Time Data Type

Data typeDescription
DATEA 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’
SQL Date and Time Data Type

3.6 Misc

Data typeDescription
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
SQL other types of data

4. SQL statements

4.1 SQL Select, From, Where

A 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 WHERE clause:

OperatorDescription
=Equal
>Greater than
>=Greater than or equal
<Less than
<=Less than or equal
!=Not equal
BETWEENBetween a certain range
LIKESearch for a pattern
INTo specify multiple possible values for a column

The WHERE clause can be combined with the AND, OR, and NOT operators. We can use the ORDER 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

The 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.

The 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...

Like UPDATE make sure you use the WHERE condition otherwise it will delete all the records in that table.

4.3 Views

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.

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button