SQL Basic Queries

INTRODUCTION

In this blog. let’s see about SQL basic queries and using queries how to create table and insert values in the table and how to alter, delete, update values in the table.

CREATE DATABASE

It is used to create a new database.

Syntax

CREATE DATABASE database_name;

Example

CREATE DATABASE college;

DROP DATABASE

It is used to delete the database.

Syntax

DROP DATABASE database_name;

OR

DROP SCHEMA database_name; --- using SCHEMA keyword to delete the database --

Example

DROP DATABASE college;

IF EXISTS

To prevent error if database is not found.

For example

DROP SCHEMA IF EXISTS college;

If we deleted the database and we use IF EXISTS to delete the database it shows a warning message. If the database is not deleted we want to delete the database using the IF EXISTS to delete the database.

USE

To select a specific database and then perform operations using this command.

Syntax

USE database name;

For example

USE college;

CREATE TABLE

To create a new table in a database.

Syntax

CREATE TABLE table_name(
Column1 datatype,
Column2 datatype,
Column3 datatype,
……..
);

Example

CREATE TABLE student(
id INT,
name VARCHAR(30),
gpa DECIMAL(3,2)
);

DESCRIBE

To describe the columns in the table.

For example

describe STUDENT;

INSERT INTO

To insert new records in a table.

Syntax

INSERT INTO table_ name VALUES (value1,value2,…);

OR

INSERT INTO table_ name VALUES
(values1, values2, values3),
(values1, values2, values3), ----- inserts more than one row -----
(values1, values2, values3);

OR

INSERT INTO student (column 1 , column2,… ) VALUES (Values1, values2…); --- inserts specific columns ------

For example

INSERT INTO student VALUES
(100,'praba', 8.9),
(102,'vani', 9.8);

ALTER

To delete, add or modify columns in an existing table.

Syntax:

ALTER TABLE table _ name
ADD column_ name datatype;

For example

ALTER TABLE student
ADD dept VARCHAR (30);

SELECT

To select data from a database.

Syntax

SELECT column1,column2,….
FROM table_ name ;

OR

SELECT * FROM table_ name;

For example

SELECT * FROM student;

SELECT name FROM student;

WHERE

WHERE is a clause. It is used to filter records.

Syntax

SELECT column1, column2 , ….
FROM table_ name
WHERE condition;

For example

SELECT * FROM student
WHERE id=100;

UPDATE

To modify the existing records in a table.

Syntax

UPDATE table_ name
SET column1=value1, column2=value2,….
WHERE condition;

For example

UPDATE student
SET gpa=9.00
WHERE id=100;

DELETE

To delete existing records in a table.

Syntax

DELETE FROM table_ name
WHERE condition;

For example

DELETE FROM student
WHERE id=102;

I hope this blog is most helpful to you.

Thank you.