0% found this document useful (0 votes)
63 views

Basics of SQL Queries: Dbms Lab (Cs593)

in our college week wise dbms are being taught . i thought of this slide a good means to learn SQL LANGUAGE from scratch

Uploaded by

Pallabi Guha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Basics of SQL Queries: Dbms Lab (Cs593)

in our college week wise dbms are being taught . i thought of this slide a good means to learn SQL LANGUAGE from scratch

Uploaded by

Pallabi Guha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Basics of SQL Queries

DBMS LAB (CS593)


Some of The Most Important SQL
Commands
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
• DATABASE Creation:
Syntax: CREATE DATABASE database_name;
E.g. : CREATE DATABASE employees;

• SHOW DATABASES;

• Use DATABASE:
Syntax: USE database_name;
E.g. : USE employees;
• Drop Database:
Syntax: DROP DATABASE database_name;
E.g. : DROP DATABASE employees;

• CREATE TABLE
Syntax:
CREATE TABLE table_name (column_name column_typ
e...);

E.g.: CREATE TABLE cus_tbl( cus_id INT NOT NULL,


cus_firstname VARCHAR(100) NOT NULL,
cus_surname VARCHAR(100) NOT NULL,
PRIMARY KEY ( cus_id ) );
• See the table structure:
Syntax: DESCRIBE table_name;
E.g.: DESCRIBE cus_tbl;
Alter table – ADD COLUMN TO AN EXISTING TABLE:

ALTER TABLE table_name ADD new_column_name column_definiti


on [ FIRST | AFTER column_name ];

Parameters:
table_name: It specifies the name of the table that you want to
modify.
new_column_name: It specifies the name of the new column that
you want to add to the table.
column_definition: It specifies the data type and definition of the
column (NULL or NOT NULL, etc).
FIRST | AFTER column_name: It is optional. It tells MySQL where in
the table to create the column. If this parameter is not specified,
the new column will be added to the end of the table.
Example:
In this example, we add a new column "cus_age" in the existing table
"cus_tbl".
ALTER TABLE cus_tbl ADD cus_age varchar(40) NOT NULL;
Add multiple columns in the table
Syntax: ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ],
ADD new_column_name column_definition
[ FIRST | AFTER column_name ],
...
;

E.g. :
ALTER TABLE cus_tbl ADD cus_address varchar(10
0) NOT NULL AFTER cus_surname, ADD cus_salar
y int(100) NOT NULL AFTER cus_age ;
MODIFY column in the table
Syntax: ALTER TABLE table_name MODIFY column_name
column_definition;

E.g.: ALTER TABLE cus_tbl MODIFY cus_surname


varchar(50) NULL;

DROP column in table


Syntax:
ALTER TABLE table_name DROP COLUMN column_name;

E.g.: ALTER TABLE cus_tbl DROP COLUMN cus_address;


RENAME column in table
Syntax: ALTER TABLE table_name
CHANGE COLUMN old_name new_name
column_definition ;
E.g.:
ALTER TABLE cus_tbl CHANGE COLUMN cus_surn
ame cus_title varchar(20) NOT NULL;

RENAME table
Syntax: ALTER TABLE table_name
RENAME TO new_table_name;

E.g: ALTER TABLE cus_tbl RENAME TO cus_table;


TRUNCATE Table
Syntax: TRUNCATE TABLE table_name;
E.g.: TRUNCATE TABLE cus_tbl;

DROP Table
Syntax: DROP TABLE table_name;
E.g.: DROP TABLE cus_tbl;
MySQL Create VIEW
Syntax:
CREATE VIEW view_name AS SELECT columns
FROM tables [WHERE conditions];

E.g.:
CREATE VIEW trainer AS SELECT course_name, cours
e_trainer FROM courses;

SELECT * FROM trainer;


Update VIEW
Syntax:
ALTER VIEW view_name AS
SELECT columns FROM table WHERE conditions;

E.g.:
ALTER VIEW trainer AS SELECT course_name,
course_trainer, course_id FROM courses;

Drop VIEW
Drop VIEW trainer;
Simple MySQL Queries
1.create database db1;
2. use db1;
3.CREATE TABLE customers (id int(10), name varchar(50),
city varchar(50), PRIMARY KEY (id));
4.ALTER TABLE customers ADD age varchar(50);
5.insert into customers values(101,'rahul','delhi');
6.update customers set name='bob', city='london' where id
=101;
7.delete from customers where id=101;
8.SELECT * from customers;
9.truncate table customers;
10. drop table customers;
MySQL WHERE Clause
Syntax:
Select field1, field2...field n from table_name where
condition(s) ;

E.g.:
1. SELECT * FROM officers WHERE address = 'Mau';
2.SELECT * FROM officers WHERE address = 'Lucknow‘
AND officer_id < 5;
3. SELECT * FROM officers WHERE address = 'Lucknow‘
OR address = 'Mau';
4.SELECT * FROM officers WHERE (address = 'Mau'
AND officer_name = 'Ajit') OR (officer_id < 5);
Distinct Clause
Syntax:
SELECT DISTINCT expressions FROM tables
[WHERE conditions];

E.g.:
1. SELECT DISTINCT address FROM officers;
2.SELECT DISTINCT officer_name, address FROM officers;
AGGREGATE FUNCTIONS

1. count() Function
SELECT COUNT (aggregate_expression) FROM table_name
[WHERE conditions];

2. sum() function
SELECT SUM(aggregate_expression) FROM tables
[WHERE conditions];

3. avg() function
SELECT AVG(aggregate_expression) FROM tables
[WHERE conditions];
4. min() function
SELECT MIN (aggregate_expression) FROM tables [
WHERE conditions];

5.max() function
SELECT MAX(aggregate_expression) FROM tables [
WHERE conditions];

6. first function
SELECT column_name FROM table_name LIMIT n;
e.g. SELECT officer_name FROM officers LIMIT 2;
7. Last function
Syntax:
SELECT column_name FROM table_name
ORDER BY column_name DESC LIMIT n;

E.g.:
SELECT officer_name FROM officers ORDER BY
officer_name DESC LIMIT 1;
MySQL ORDER BY Clause
Syntax:
SELECT expressions FROM tables [WHERE conditions] ORD
ER BY expression [ ASC | DESC ];

E.g.:
1.SELECT * FROM officers WHERE address = 'Lucknow'
ORDER BY officer_name;
2. SELECT * FROM officers WHERE address = 'Lucknow'
ORDER BY officer_name ASC;
3. SELECT * FROM officers WHERE address = 'Lucknow'
ORDER BY officer_name DESC;
4.SELECT officer_name, address FROM officers WHERE
officer_id < 5 ORDER BY officer_name DESC, address ASC;
MySQL GROUP BY Clause
• The MYSQL GROUP BY Clause is used to collect data from multiple records and
group the result by one or more column. It is generally used in a SELECT
statement.
• You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG
etc. on the grouped column.

• SELECT address, COUNT(*) FROM officers GROUP BY address;


• SELECT emp_name, SUM(working_hours) AS "Total working hours" FROM em
ployees GROUP BY emp_name;
• SELECT emp_name, MIN(working_hours) AS "Minimum working hour" FROM
employees GROUP BY emp_name;
• SELECT emp_name, MAX (working_hours) AS "Minimum working hour" FROM
employees GROUP BY emp_name;
• SELECT emp_name, AVG(working_hours) AS "Average working hour" FROM e
mployees GROUP BY emp_name;

You might also like