Basics of SQL Queries: Dbms Lab (Cs593)
Basics of SQL Queries: Dbms Lab (Cs593)
• 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...);
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;
RENAME table
Syntax: ALTER TABLE table_name
RENAME TO new_table_name;
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;
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.