SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. It allows users to create, read, update, and delete database records. SQL is widely used for querying data, defining schemas, and ensuring data integrity in systems like MySQL, Oracle, and PostgreSQL. While using Microsoft SQL use “ ` “ symbol when using the name of the column.
• To Create a Table
To define a table structure, use the following SQL query:
create database first;
show databases;
use first;
Create table employee(ssn varchar(10) primary key, Name char(30) Not Null, phone int, doj Date Not Null);
(system,usual small05)
• Printing All Records in a Table
To display the table content, use:
SELECT * FROM first;
• Inserting Data into the Table
To insert new records into the table:
insert into employee
2 values(2,'tommy shelby',91,'12-may-24'),
(3,'john',91,'12-may-2024');
op>1 row created.
• Updating a Column with New Data
To modify existing data in a table:
update employee
2 set age='21'
3 where name='john doe';
• Deleting a Specific Row
To remove a row from the table:
SQL> delete
2 from employee
3 where ssn='3';
• Dropping the Entire Table
To delete the table completely:
drop table employee;
• Updating All Values in a Row
To update all values for a specific row:
SQL> update employee
2 set phone='30';
General Syntax for SELECT Statement
select attribute
from table_name
where condition;
• Adding a New Column to an Existing Table
To add and set values in a new column:
update employee
2 set age=30
3 where ssn=1;
4 ;
• Selecting Data Using Alias for Tables
To refer to tables with aliases:
SQL> select e.name
2 from employee e,emp em
3 where e.ssn=em.ssn;
• Union Operator
To combine results of two queries:
SQL> select * from a
2 union
3 select * from b;
• Intersection Operator
To return common records between two queries:
SQL> select * from a
2 intersect
3 select * from b;
Example with conditions:
SQL> select a.x
2 from a a,b b
3 where a.x=b.x and b.y>2;
• Nested Queries (Subqueries)
To use queries inside another query:
SQL> select x
2 from a
3 where y in (select y from b where y<=10);
Here the function checks if y
is equal to b.y
.
• Installing MySQL on WSL
To set up MySQL on WSL (Windows Subsystem for Linux):
sudo apt update
sudo apt install mysql-server
• Running MySQL
To start and check MySQL status:
sudo systemctl status MySQL # Check if active and running
sudo MySQL
create database first;
show databases;
use first;
describe first;
• Adding a New Column
To insert a new column into a table:
Alter table table_name add column_name datatype;
• Deleting Specific Rows
To delete data from a table:
delete from first where id==1;
• Bulk Update in a Table
To update multiple rows conditionally:
update first
-> set id=case
-> when `index`=1 then 101
-> when `index`=2 then 102
-> when `index`=3 then 103
-> when `index`=4 then 104
-> else id
-> end;
• Union Query
To combine index
and name
from two tables:
select `index`,`name` from first
-> union
-> select `index`,`name` from second;
• Intersection Query
To find common records:
select `index` from first intersect select `index` from second;
• Minus Operation
To find records present in one table but not in another:
select * from first where `index` not in(select `index` from second);
• Between Operator
To fetch rows in a specified range:
select `name` from first where `index` between 1 and 3;
• Calculated Value in SELECT Query
To calculate and display new values based on columns:
SELECT `name`, (`id` + `id` + 3) AS calculated_value
-> FROM first
-> WHERE `index` > 1;
Here, calculated_value
applies a new condition.
• Nested Queries (Example)
To use subqueries for advanced queries:
select `name`
from emp
where fix=(select `fid`
from std
where `sal`>(select `sal`
from std
where `dept` = 'AIML'));
• Aggregate Functions
These functions help summarize query results:
Sum (int)
Avg (int)
MAX (int,char)
MIN (int,char)
Count (int,float,char)
Example:
select `name`
from first
when 2<=count(select `D ssn`
from dep D emp E
when D `essn`=E `Essn`);
• Grouping Query Results
To group data and apply conditions:
select
from
where
group by
(e.g.,group by dno
)having
(e.g.,having count(*)>=1
)order by
(e.g., ascending order)
Example:
select `dno`,count(*),avg(`salary`)
from emp
grouping by `dno`
having count(*)>=1
order by asc;
Hope you learnt something today
Happy Coding…