Structured Query Language (SQL) is a standardized language that allows you
to perform operations on a database, such as creating entries, reading content, updating
content, and deleting entries.
SQL is supported by all most any database you will likely use, and it allows you to write
database code independently of the underlying database.
This tutorial gives an overview of SQL, which is a pre-requisite to understand JDBC concepts.
This tutorial gives you enough SQL to be able to Create, Read, Update, and
Delete (often referred to as CRUD operations) data from a database.
For a detailed understanding on SQL, you can read our MySQL
The CREATE DATABASE statement is used for creating a new database. The syntax is:
SQL> CREATE DATABASE DATABASE_NAME;
The Below SQL statement creates a Database named EMP:
SQL> CREATE DATABASE EMP;
The DROP DATABASE statement is used for deleting an existing database. The syntax is:
SQL> DROP DATABASE DATABASE_NAME;
Note: To create or drop a database you should have administrator privilege on your
database server. Be careful, deleting a database would loss all the data stored in database.
The CREATE TABLE statement is used for creating a new table. The syntax is:
SQL> CREATE TABLE table_name
The Below SQL statement creates a table named Friends with four columns:
SQL> CREATE TABLE Friends
id INT NOT NULL,
age INT NOT NULL,
PRIMARY KEY ( id )
The DROP TABLE statement is used for deleting an existing table. The syntax is:
SQL> DROP TABLE table_name;
The Below SQL statement deletes a table named Friends:
SQL> DROP TABLE Friends;
The syntax for INSERT looks similar to the following, where column1, column2, and so on represent
the new data to appear in the
SQL> INSERT INTO table_name VALUES (column1, column2, ...);
The Below SQL INSERT statement inserts a new row in the Friends database created earlier:
SQL> INSERT INTO Friends VALUES (100, 18, 'Sophia', 'Lomela');
The SELECT statement is used to retrieve data from a database. The syntax for SELECT is:
SQL> SELECT column_name, column_name, ...
The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as
well as the BETWEEN and LIKE operators.
The Below SQL statement selects the age, first and last columns from the Friends table where id
column is 100:
SQL> SELECT first, last, age
WHERE id = 100;
The Below SQL statement selects the age, first and last columns from the Friends table where first
column contains Sophia:
SQL> SELECT first, last, age
WHERE first LIKE '%Sophia%';
The UPDATE statement is used to update data. The syntax for UPDATE is:
SQL> UPDATE table_name
SET column_name = value, column_name = value, ...
The Below SQL UPDATE statement changes the age column of the Friend whose id is 100:
SQL> UPDATE Friends SET age=20 WHERE id=100;
The DELETE statement is used to delete data from tables. The syntax for DELETE is:
SQL> DELETE FROM table_name WHERE conditions;
The Below SQL DELETE statement delete the record of the Friend whose id is 100:
SQL> DELETE FROM Friends WHERE id=100;
Your Query was successfully sent!