Hello, readers in this article we will discuss all basic SQL statements with examples. SQL is an important language that you should know in order to communicate with RDBMS like phpMyAdmin.

Here we will discuss topics like creating a database, deleting a database, creating a table, deleting a table, modifying a table, inserting data into a table, reading data from a table, editing data in a table, and deleting data in a table. This is going to be a vital resource, so bookmark this page now so that you can access this information whenever you need it.

Now let’s start with the topic.

Learn All Basic SQL Statements With Examples In One Place

All Basic SQL Statements With Examples

Now that you know this article’s content, let’s start with creating a database.

Creating a database

In order to create a database we use a keyword CREATE DATABASE that is all clear with the name itself.

Syntax

CREATE DATABASE databasename;

Example

CREATE DATABASE myDB;

The above example will create a database named ‘myDB’ in your respective DBMS.

Dropping database (Deleting database)

In order to delete a database we use a keyword DROP DATABASE that is also very clear with its name itself.

Syntax

DROP DATABASE databasename;

Example

DROP DATABASE myDB;

The above example will delete/ drop the database named ‘myDB’ in your respective DBMS.

Creating a table

In order to create a table with SQL you will need to use a keyword CREATE TABLE which is also completely clear with its naming convention.

Syntax

CREATE TABLE tablename (column1 datatype, column2 datatype, column3 datatype, ...);

Example

CREATE TABLE data (id int, name varchar(20), email varchar(50));

The above example will create a table named data with 3 columns titled ‘id’, ‘name’, and ’email’ with data types ‘int’, ‘varchar’, and ‘varchar’ respectively.

Adding a column in the table

In order to add a column in the table we need to use a keyword called ALTER TABLE which is also correctly named.

Syntax

ALTER TABLE tablename ADD columnname datatype;

Example

ALTER TABLE data ADD phone int;

The above example will add a column named ‘phone’ with a datatype of ‘int’.

Deleting a column in the table

In order to delete a column we use the same keywords as adding a column.

Syntax

ALTER TABLE tablename DROP COLUMN columnname;

Example

ALTER TABLE data DROP COLUMN phone;

The above example will delete the column named ‘phone’ that we just added in the above heading.

Deleting a table

In order to delete a table we use the keyword DROP TABLE which is also nicely named.

Syntax

DROP TABLE tablename;

Example

DROP TABLE data;

The above example will delete the table named ‘data’ from your database.

Now if you are following this article Basic SQL Statements With Examples you should have built up an idea that SQL Statements are really easy to understand and memorize and it is because they look familiar and the keywords do what they are named, unlike some languages.

Inserting data into a table

In order to insert data into a table we use the keyword INSERT INTO which is also quite nicely named.

Syntax

INSERT INTO tablename (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
INSERT INTO data (id, name, email) VALUES ('1', 'Hari', 'hari@hari.com');

The above example will insert data with id = 1, name = Hari, and email = hari@hari.com into the table named ‘data’ which we created above in this article Basic SQL Statements With Examples.

Selecting data from the table

Selecting actually refers to fetching data from the database table. We use the keyword SELECT to actually fetch data.

Syntax

SELECT * FROM tablename;

Example

SELECT * FROM data;

The above SQL statement will fetch every column and every row from the database table named ‘data’.

Selecting data from the table with conditions

In order to select data with conditions we use a condition clause WHERE in the above SQL statement of SELECT.

Syntax

SELECT column1, column2, ... FROM tablename WHERE condition;

Example

SELECT id, name FROM data WHERE email = 'hari@hari.com';

The above SQL statement will return the id and name of the table ‘data’ where the email is equal to ‘hari@hari.com’.

Updating data in the table (Modifying data)

In order to update data we use a nicely named keyword UPDATE.

Syntax

UPDATE tablename SET column1 = 'value1', column2 = 'value2', ... WHERE condition;

Example

UPDATE data SET name = 'Ram', email = 'ram@ram.com' WHERE id = '1';

The above SQL statement will update the name and email of table ‘data’ where the id is equal to 1.

Deleting data from the table

Until now we have used the keyword DROP to delete so you may think we use the same keyword to delete the data but it’s not the case. We use the keyword DELETE itself to delete the data from the table.

Syntax

DELETE FROM tablename WHERE condition;

Example

DELETE FROM data WHERE id = '1';

The above SQL statement will delete the data from the table ‘data’ where the id is equal to 1.

So, this was my article for Basic SQL Statements with examples. If you liked the content then it would be fabulous if you subscribe to our newsletter.

Subscribe to our newsletter!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *