SQL

Create Table Insert Data Into Table and Select Data in SQL

Structured Query Language (SQL) is standard language to manipulate databases. MySQL, MariaDB, MS-Access and all popular Database Management System software use SQL. In this post, I will demonstrate how to create a table, insert data into table and select columns and rows.

In this example, I will example of class table of school database.

So, first you will create a database school using below command.

CREATE DATABASE school;

Therefore, you will use database school using USE command i.e..

USE school;

Thereafter, you will use CREATE TABLE command to create a table (csclass) inside school database using the following command.

CREATE TABLE csclass(Name CHAR(20), regno INTEGER, marks INTEGER);

In the above table, Name, regno and marks are columns of csclass table, their data types are character, integer and integer respectively.

Table is created, now you can insert data into it using INSERT INTO command with VALUES function.

INSERT INTO csclass VALUES(‘Manoj’, 1001, 67);

The first row is inserted using INSERT INTO command.

INSERT INTO csclass VALUES(‘Pappu’, 1002, 65);

The second row inserted using INSERT INTO command.

In the same way you can create as many rows as you need using INSERT INTO command.

INSERT INTO csclass VALUES(‘Nitish’, 1003, 70);
INSERT INTO csclass VALUES(‘Audhesh’, 1004,75);
INSERT INTO csclass VALUES(‘Arvind’, 1005, 85);

SQL

Now table csclass is created which has 3 columns and 5 rows.

To see rows and columns of table csclass you will require SELECT command with WHERE clause.

To all rows and all columns you will write command.

SELECT Name, regno, marks FROM csclass;
You can also write.
SELECT * FROM csclass;
* means all columns.
SQL

If you want only two columns namely Name and regno, you will write the command.
SELECT Name, regno FROM csclass;

SQL

Till now, you have learnt how select columns of the table csclass, now you will learn how to select rows using WHERE clause.

For example if you want to see list of students whose marks are greater than 70, then you will write the command.

SELECT Name, regno, marks FROM csclass WHERE marks >70;

SQL

Further, you can use relational operators > ( Greate than), < (Less Than), <=(Less Than or equal to), >=( Greatr than or equal to), =(Equal to), and <>(Not equal to) operator . And logical operators and, or and not.

 

 

 

 

 

 

 

 

 

Leave a Comment

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

©Postnetwork-All rights reserved.