SQL Queries For Beginners

SQL Queries For Beginners

1.      Create Database

We use “CREATE DATABASE” Statement to create a new database in SQL.

Syntax

CREATE DATABASE database_name;

Example

CREATE DATABASE Students_DDBMS;

Exampleshot

2.      Create Table

We use “CREATE TABLE” Statement to create a new table in a database.

Syntax

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   (and so on)
);

Example

CREATE TABLE studentinfo (
    roll int,
    FirstName varchar(255),
    LastName varchar(255),
    Class varchar(5),
    CGPA numeric(3,2),
    City varchar(255)
);

Exampleshot

3.      Alter Table

We use “ALTER TABLE” statement to add a new column in a table.

Syntax

ALTER TABLE table_name
ADD column_name datatype;

Example

ALTER TABLE studentinfo
ADD email varchar(100);

Exampleshot

4.      Insert Into

We use “INSERT INTO” statement to add a new record in a table.

Syntax

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Example

INSERT INTO studentinfo (roll, FirstName,LastName, Class, CGPA,City,email) 
VALUES (5502, 'Asad', 'Abbas', 'MCS 4', 3.02, 'Sargodha', 'asad@zainrhamid.com');

Exampleshot

5.      Update

We use “UPDATE” statement to modify the existing records in a table.

Syntax

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

Example

UPDATE studentinfo
SET CGPA = 3.00
WHERE roll=5562;

Exampleshot

6.      Delete

We use “DELETE” statement to delete records in a table.

Syntax

DELETE FROM table_name WHERE condition;

Example

DELETE FROM studentinfo WHERE CGPA=3.35;

Exampleshot

7.      Select

We use “SELECT” statement to select records from a table or database.

Syntax

SELECT column1, column2, ...
FROM table_name;

Example

SELECT * FROM studentinfo;(If you want to select complete table)

Exampleshot

8.      Min and Max

We use “MIN()” function to select smallest value from a selected column.

We use “MAX()” function to select largest value from a selected column.

Syntax of MIN()

SELECT MIN(column_name)
FROM table_name
WHERE condition;

Syntax of MAX()

SELECT MAX(column_name)
FROM table_name
WHERE condition;

Example of MIN()

select min(cgpa) as Lowest from studentinfo;

Example of MAX()

select max(cgpa) as Highest from studentinfo;

Exampleshot of MIN()

Exampleshot of MAX()

Read Also: Computer Processors Takes On Mobile Processors

9.      Count, Avg, Sum

We use “COUNT()” function to get the number of rows that matches a specified criteria.

We use “AVG()” function to get the average value of a numeric column.

We use “SUM()” function to get the sum of the numeric column.

Syntax of COUNT()

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Syntax of AVG()

SELECT AVG(column_name)
FROM table_name
WHERE condition;

Syntax of SUM()

SELECT SUM(column_name)
FROM table_name
WHERE condition;

Example of COUNT()

select count(roll) as 'Number of Records' from studentinfo;

Example of AVG()

SELECT AVG(CGPA) AS 'Average of CGPA' FROM studentinfo;

Example of SUM()

SELECT SUM(CGPA) AS 'Average of CGPA' FROM studentinfo;

Exampleshot of COUNT()

Exampleshot of AVG()

Exampleshot of SUM()

10. Like

We use “LIKE” statement in a WHERE clause to search for a specified pattern in a column.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

Example

SELECT * FROM studentinfo WHERE FirstName LIKE 'x%';

Exampleshot

11. In

We use “IN” statement to specify multiple values in a WHERE clause.

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example

SELECT * FROM studentinfo
WHERE City IN ('Sargodha', 'Rawalpindi');

Exampleshot

12. Between

We use “BETWEEN” statement to selects values within a given range.

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example

SELECT * FROM studentinfo
WHERE CGPA BETWEEN 2.6 AND 3.1;

Exampleshot

13. Drop Table

We use “DROP TABLE” statement to drop an existing table in a database.

Syntax

DROP TABLE table_name;

Example

DROP TABLE studentinfo;

Exampleshot

Leave a Reply