0% found this document useful (1 vote)
3K views

Simple Queries in SQL

SQL is a standardized programming language used to manage relational databases and perform operations on the data. It allows users to create, modify, and query databases. SQL commands include CREATE, INSERT, UPDATE, DELETE, and SELECT. The SELECT statement is used to query data from tables and supports operators like BETWEEN, AND, and aggregate functions like AVG(), MAX(), MIN() to analyze data. Column constraints like PRIMARY KEY, UNIQUE, NOT NULL, and DEFAULT can be applied when creating tables.

Uploaded by

Aryan Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views

Simple Queries in SQL

SQL is a standardized programming language used to manage relational databases and perform operations on the data. It allows users to create, modify, and query databases. SQL commands include CREATE, INSERT, UPDATE, DELETE, and SELECT. The SELECT statement is used to query data from tables and supports operators like BETWEEN, AND, and aggregate functions like AVG(), MAX(), MIN() to analyze data. Column constraints like PRIMARY KEY, UNIQUE, NOT NULL, and DEFAULT can be applied when creating tables.

Uploaded by

Aryan Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SIMPLE QUERIES IN SQL

• INTRODUCTION

- SQL (Structured Query Language) is a standardized programming


language that's used to manage relational databases and perform
various operations on the data in them. Also known
as SQL databases, relational systems comprise a set of tables
containing data in rows and columns.

- SQL enables the following : (i) creating/modifying a database’s


structure (ii) changing security settings for system (iii) Permitting
users for working on database or tables (iv) Querying database (v)
Inserting / Modifying / Deleting the database contents.
My SQL Elements
• The basic elements are :-

1. Literals .
2. Datatypes.
3. Nulls.
4. Comments.
Literals
- Literals, in generals, refers to a fixed data value. This fixed data value
may be of character type or numeric literals. For example, ‘Parag Jain’,
17, and 18 are all literals.
- Numbers that are not enclosed in quotation marks are numeric
literals e.g., 22, 18, 17, 3027, 2021 are all numeric literals.

- Numeric literals can either be integer literals i.e., without any


decimals or be real literals i.e., with a decimal point e.g., 17 is an
integer literals but 17.0 and 17.5 are real literals.
Data Types
- Data types are means to identify the type of data and associated
operations for handling it. A value’s datatype associates a fixed set of
properties with the value.
- MySQL uses many different data types, divided into three categories :
NUMERIC DATE AND TIME, AND STRINGS TYPES
INT. DATE. CHAR.
TINYINT. DATETIME. VARCHAR.
SMALLINT. TIMESTAMP. BLOB OR TEXT.
Null values
- If a column in a row has no value, then column is said to be null,
or to contain a null.

Comments
- A comment is a text that is not executed ; it is only for
documentation purpose.
SQL Command syntax
- SQL provides a predefined set of commands that are
1. KEYWORDS .
2. STATEMENTS.
3. CLAUSES.
4. ARGUMENTS.
commands in SQL are not case sensitive and all commands are execute
in mysql>
Creation of database
• For making database we have to execute this command :-
Mysql> CREATE DATABASE <database name>;
For example : create database school;
Use of database
• MySQL> use <database name>;
• For ex: use school
Table creation
For making table in database we’ve to execute commands which are as
follows :-

MySQL> Create table student(roll int(4) Primary key, name char(20),


age int(2), city char(10));
Manuplation in table structure

• for ex:
Mysql> alter table student add (class int(2));
DESC Command
• This command is used for viewing structure of the table.
• Mysql> DESC <table name> ;
For example :
Drop command

Mysql> drop table <table name>;


The select command
• A query is a command that is given to produce certain specified
information from the database table(s).
Select statement is used as given below:
SELECT <column name> [, <column name >, . . . ]
FROM <table name>;
For example-
Mysql> Select name, owner, Species
-> FROM pet;
Selecting all columns
Select * from <table name>;
example:
MySQL> select * from student;
Some SQL Commands
BETWEEN
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;
The BETWEEN operator is used to filter the result set within a certain
range. The values can be numbers, text or dates.

DELETE
DELETE FROM table_name
WHERE some_column = some_value;
DELETE statements are used to remove rows from a table.
INSERT
INSERT INTO table_name (column_1, column_2, column_3)
VALUES (value_1, 'value_2', value_3);
INSERT statements are used to add a new row to a table.

Handling Nulls

IFNULL
MySQL> SELECT name, birth, IFNULL (death, “alive”)
From pet;
SQL : Manipulation
CREATE TABLE
Statement

The CREATE TABLE statement is used to create a new


table in a database. It allows one to specify the name of
the table and the name of each column in the table.

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype);
INSERT
Statement

The INSERT INTO statement is used to add a new record (row) to a table.

It has two forms as shown in the code block:

Insert values based on the order of the columns in the table.


Define the columns to insert values into.
-- Insert into columns in order:
INSERT INTO table_name
VALUES (value1, value2, value3);

-- Insert into columns by name:


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

The UPDATE statement is used to edit records (rows) in a table. It usually


includes a SET clause that indicates the column to edit and a WHERE
clause for specifying which record(s) should be updated.

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE some_column = some_value;
Column Constraints
Column constraints are the rules applied to the values of individual columns:

• PRIMARY KEY constraint can be used to uniquely identify the row.


• UNIQUE columns have a different value for every row.
• NOT NULL columns must have a value.
• DEFAULT assigns a default value for the column when no value is specified.
There can be only one PRIMARY KEY column per table and multiple UNIQUE
columns.

CREATE TABLE student (


id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
grade INTEGER NOT NULL,
age INTEGER DEFAULT 10));
BETWEEN Operator
• The BETWEEN operator can be used to filter by a range of values. The
range of values can be text, numbers or date data. The given query
will match any movie made between the years 1980 and 1990,
inclusive.

SELECT *
FROM movies
WHERE year BETWEEN 1980 AND 1990;
AND Operators

• The AND operator allows multiple conditions to be combined.


Records must match both conditions that are joined by AND to be
included in the result set. The example query will match any car that
is blue and made after 2014.

SELECT model
FROM cars
WHERE color = 'blue'
AND year > 2014;
SQL : Aggregate Functions
AVG()
Aggregate Function

The AVG() aggregate function returns the average value in a column.


For instance, to find the average salary for the employees who have
less than 5 years of experience, the given query can be used.

SELECT AVG(salary)
FROM employees
WHERE experience < 5;
MAX()
Aggregate Function

The MAX() aggregate function in SQL takes the name of a column as an


argument and returns the largest value in a column. The given query
will return the largest value from the amount column.

SELECT MAX(amount)
FROM transactions;
MIN()
Aggregate Function

The MIN() aggregate function in SQL returns the smallest value in a


column. For instance, to find the smallest value of the amount column
from the table named transactions, the given query can be used.

SELECT MIN(amount)
FROM transactions;
THE END.
Presented by – Aryan dev.
Class – 11 commerce.
th

You might also like