By : Karunakaran
<ul><li>SQL stands for Structured Query Language </li></ul><ul><li>SQL lets you access and manipulate databases </li></ul>...
<ul><li>SQL can execute queries against a database </li></ul><ul><li>SQL can retrieve data from a database </li></ul><ul><...
<ul><li>A database most often contains one or more tables. Each table is identified by a name (e.g. “employee&quot; or &qu...
<ul><ul><li>mysql> create table employee1 </li></ul></ul><ul><ul><li>-> (emp_id int, </li></ul></ul><ul><ul><li>-> emp_nam...
<ul><li>SQL   (Structured Query Language)  is a syntax for executing queries. But the SQL language also includes a syntax ...
<ul><li>The  Data Definition Language (DDL)  part of SQL permits database tables to be created or deleted. We can also def...
<ul><li>The  SELECT  statement is used to select data from a table. The tabular result is stored in a result table (called...
<ul><li>mysql> select * from employee1; </li></ul>emp_id emp_name emp_salary 1 komal  10000 2 kavita  20000 3 komalpreet 3...
<ul><li>The INSERT INTO statement is used to insert new rows into a table. </li></ul><ul><li>Syntax:  INSERT INTO table_na...
<ul><li>The UPDATE statement is used to modify the data in a table. </li></ul><ul><li>Syntax: </li></ul><ul><ul><li>UPDATE...
emp_id emp_name emp_salary 1 Komal 10000 2 Kavita  20000 3 hello 30000
<ul><li>The DELETE statement is used to delete rows in a table. </li></ul><ul><li>Syntax: </li></ul><ul><ul><li>DELETE FRO...
<ul><li>The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. </li></ul><ul><li>Syntax...
<ul><li>The DROP  INDEX  statement is used to delete an index in a table. </li></ul><ul><li>Syntax:  </li></ul><ul><li>DRO...
<ul><li>The SELECT INTO statement selects data from one table and inserts it into a different table. </li></ul><ul><li>The...
<ul><li>E.g.  </li></ul><ul><li>mysql>select* from person; </li></ul><ul><ul><li>create table person10 </li></ul></ul><ul>...
mysql> insert into person10 -> select * from person; mysql> select * from person10; Address City State 1 Ali Raj 2 Kumar H...
<ul><li>SQL has many built-in functions for performing calculations on data. </li></ul><ul><li>SQL Aggregate Functions: </...
<ul><li>SQL Scalar functions </li></ul><ul><ul><li>SQL scalar functions return a single value, based on the input value. <...
emp_id emp_name emp_salary 1 Savita 10000 2 Kavita 20000 3 Komal 30000
1 )  SELECT AVG(column name) FROM table_name select avg (emp salary) as order average from employee1; 2 )  SELECT COUNT(*)...
3)  SELECT FIRST(column_name) FROM table_name select emp_name from employee1 order by emp id limit 1; 4)  SELECT LAST(colu...
5)  SELECT MAX(column_name) FROM table_name Eg:  select max(emp_salary) from employee1; Ouput: 30000 6 )  SELECT MIN(colum...
9 )  SELECT LCASE(column_name) FROM table_name Eg:  select lcase (emp_name) from employee1; Output: savita kavita komal
<ul><li>SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these...
1) Join SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name E...
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.P_Id=Orders.P_Id; Las...
LastName FirstName OrderNo Hansen   Ola   22456 Hansen  Ola   24562 Pettersen Kari   77895 Pettersen Kari   44678 Svendson...
LastName FirstName OrderNo Hansen   Ola   22456 Hansen   Ola   24562 Pettersen Kari   77895 Pettersen Kari   44678 34764 4...
LastName FirstName OrderNo Hansen   Ola   22456 Hansen   Ola  24562 Pettersen Kari   77895 Pettersen Kari   44678 Svendson...
 
Upcoming SlideShare
Loading in …5
×

Sql

2,048 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,048
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
218
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Sql

  1. 1. By : Karunakaran
  2. 2. <ul><li>SQL stands for Structured Query Language </li></ul><ul><li>SQL lets you access and manipulate databases </li></ul><ul><li>It is the most commonly used relational database language today. </li></ul>
  3. 3. <ul><li>SQL can execute queries against a database </li></ul><ul><li>SQL can retrieve data from a database </li></ul><ul><li>SQL can insert records in a database </li></ul><ul><li>SQL can update records in a database </li></ul><ul><li>SQL can delete records from a database </li></ul><ul><li>SQL can create new databases </li></ul><ul><li>SQL can create new tables in a database </li></ul><ul><li>SQL can create stored procedures in a database </li></ul><ul><li>SQL can create views in a database </li></ul><ul><li>SQL can set permissions on tables, procedures, and views. </li></ul>
  4. 4. <ul><li>A database most often contains one or more tables. Each table is identified by a name (e.g. “employee&quot; or &quot;Orders&quot;). </li></ul><ul><li>To begin, you must first CREATE a database using the following SQL statement: </li></ul><ul><li>CREATE DATABASE database_name </li></ul><ul><li>eg: create database employee; </li></ul><ul><li>Depending on the version of SQL being used the following statement is needed to begin using the database: </li></ul><ul><li>USE database_name </li></ul><ul><li>eg: use employee; </li></ul>
  5. 5. <ul><ul><li>mysql> create table employee1 </li></ul></ul><ul><ul><li>-> (emp_id int, </li></ul></ul><ul><ul><li>-> emp_name varchar(25), </li></ul></ul><ul><ul><li>-> emp_salary int); </li></ul></ul>emp_id emp_name emp_salary int varchar(25) int
  6. 6. <ul><li>SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. </li></ul><ul><li>These query SQL and update commands together form the Data Manipulation Language (DML) part of SQL: </li></ul><ul><ul><li>SELECT - extracts data from a database table </li></ul></ul><ul><ul><li>UPDATE - updates data in a database table </li></ul></ul><ul><ul><li>DELETE - deletes data from a database table </li></ul></ul><ul><ul><li>INSERT INTO - inserts new data into a database table </li></ul></ul>
  7. 7. <ul><li>The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. </li></ul><ul><li>The most important DDL statements in SQL are:  </li></ul><ul><li>CREATE TABLE - creates a new database table </li></ul><ul><li>ALTER TABLE - alters (changes) a database table </li></ul><ul><li>DROP TABLE - deletes a database table </li></ul>
  8. 8. <ul><li>The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set). </li></ul><ul><li>Syntax: </li></ul><ul><li>SELECT * FROM table_name; </li></ul><ul><li>(The * Operator asks for every column in the table) </li></ul><ul><li>(Semicolon is the standard way to separate each SQLstatement in database systems that allow more than one SQL statement to be executed in the same call to the server.) </li></ul>
  9. 9. <ul><li>mysql> select * from employee1; </li></ul>emp_id emp_name emp_salary 1 komal 10000 2 kavita 20000 3 komalpreet 30000
  10. 10. <ul><li>The INSERT INTO statement is used to insert new rows into a table. </li></ul><ul><li>Syntax: INSERT INTO table_name VALUES (value1, value2,....) </li></ul><ul><li>For example: </li></ul><ul><li>mysql> insert into employee1 </li></ul><ul><li>-> values(1,‘komal',10000); </li></ul>emp_id emp_name emp_salary 1 komal 10000
  11. 11. <ul><li>The UPDATE statement is used to modify the data in a table. </li></ul><ul><li>Syntax: </li></ul><ul><ul><li>UPDATE table_name </li></ul></ul><ul><ul><li>SET column_name = new_value </li></ul></ul><ul><ul><li>WHERE column_name = some_value </li></ul></ul><ul><li>mysql> update employee1 </li></ul><ul><li> -> set emp_name=‘hello’ </li></ul><ul><li> -> where emp_id=3; </li></ul>
  12. 12. emp_id emp_name emp_salary 1 Komal 10000 2 Kavita 20000 3 hello 30000
  13. 13. <ul><li>The DELETE statement is used to delete rows in a table. </li></ul><ul><li>Syntax: </li></ul><ul><ul><li>DELETE FROM table_name </li></ul></ul><ul><ul><li>WHERE column_name = some_value </li></ul></ul><ul><li>mysql> delete from employee1 </li></ul><ul><li> -> where emp_id=2 and emp_name=‘kavita'; </li></ul>emp_id emp_name emp_salary 1 komal 10000 3 hello 30000
  14. 14. <ul><li>The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. </li></ul><ul><li>Syntax: </li></ul><ul><li>ALTER TABLE table_name ADD column_name datatype </li></ul><ul><li>Eg: mysql> alter table employee1 </li></ul><ul><li>-> add dateofbirth date; </li></ul>emp_id emp_name emp_salary Dateof birth 1 Savita 10000 2 Kavita 20000 3 hello 30000
  15. 15. <ul><li>The DROP INDEX statement is used to delete an index in a table. </li></ul><ul><li>Syntax: </li></ul><ul><li>DROP TABLE table_name </li></ul><ul><li>Eg: drop table employee1; </li></ul>
  16. 16. <ul><li>The SELECT INTO statement selects data from one table and inserts it into a different table. </li></ul><ul><li>The SELECT INTO statement is most often used to create backup copies of tables. </li></ul><ul><li>Syntax: </li></ul><ul><li>INSERT INTO NEW_TABLE </li></ul><ul><ul><li>SELECT* FROM OLD_TABLE </li></ul></ul>
  17. 17. <ul><li>E.g. </li></ul><ul><li>mysql>select* from person; </li></ul><ul><ul><li>create table person10 </li></ul></ul><ul><li>-> (address varchar(30), </li></ul><ul><li>-> city varchar(10), </li></ul><ul><li>-> state varchar(20)); </li></ul>p_id lastname firstname 1 Ali Raj 2 Kumar Hansen 3 Peterson Ola
  18. 18. mysql> insert into person10 -> select * from person; mysql> select * from person10; Address City State 1 Ali Raj 2 Kumar Hansen 3 Peterson Ola
  19. 19. <ul><li>SQL has many built-in functions for performing calculations on data. </li></ul><ul><li>SQL Aggregate Functions: </li></ul><ul><ul><li>SQL aggregate functions return a single value, calculated from values in a column. </li></ul></ul><ul><ul><li>Useful aggregate functions: </li></ul></ul><ul><ul><li>AVG() - Returns the average value </li></ul></ul><ul><ul><li>COUNT() - Returns the number of rows </li></ul></ul><ul><ul><li>FIRST() - Returns the first value </li></ul></ul><ul><ul><li>LAST() - Returns the last value </li></ul></ul><ul><ul><li>MAX() - Returns the largest value </li></ul></ul><ul><ul><li>MIN() - Returns the smallest value </li></ul></ul><ul><ul><li>SUM() - Returns the sum </li></ul></ul>
  20. 20. <ul><li>SQL Scalar functions </li></ul><ul><ul><li>SQL scalar functions return a single value, based on the input value. </li></ul></ul><ul><ul><li>Useful scalar functions: </li></ul></ul><ul><ul><li>UCASE() - Converts a field to upper case </li></ul></ul><ul><ul><li>LCASE() - Converts a field to lower case </li></ul></ul><ul><ul><li>MID() - Extract characters from a text field </li></ul></ul><ul><ul><li>LEN() - Returns the length of a text field </li></ul></ul><ul><ul><li>ROUND() - Rounds a numeric field to the number of decimals specified </li></ul></ul><ul><ul><li>NOW() - Returns the current system date and time </li></ul></ul><ul><ul><li>FORMAT() - Formats how a field is to be displayed </li></ul></ul>
  21. 21. emp_id emp_name emp_salary 1 Savita 10000 2 Kavita 20000 3 Komal 30000
  22. 22. 1 ) SELECT AVG(column name) FROM table_name select avg (emp salary) as order average from employee1; 2 ) SELECT COUNT(*) FROM table_name select count(emp_name) as emp_namesavita from employee1 where emp_name='savita'; Order Average 20000.0000 emp_namesavita 1
  23. 23. 3) SELECT FIRST(column_name) FROM table_name select emp_name from employee1 order by emp id limit 1; 4) SELECT LAST(column_name) FROM table_name select emp_name from employee1 order by emp id desc limit 2; emp_name Savita emp_name Komal Kavita
  24. 24. 5) SELECT MAX(column_name) FROM table_name Eg: select max(emp_salary) from employee1; Ouput: 30000 6 ) SELECT MIN(column_name) FROM table_name Eg: select min(emp_salary) from employee1; Output: 10000 7 ) SELECT SUM(column_name) FROM table_name Eg: select sum(emp_salary) from employee1; Output: 60000 8) SELECT UCASE(column_name) FROM table_name Eg: select ucase(emp_name) from employee1; Output: SAVITA KAVITA KOMAL
  25. 25. 9 ) SELECT LCASE(column_name) FROM table_name Eg: select lcase (emp_name) from employee1; Output: savita kavita komal
  26. 26. <ul><li>SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables. </li></ul><ul><li>Different SQL JOINs </li></ul><ul><ul><li>JOIN: Return rows when there is at least one match in both tables </li></ul></ul><ul><ul><li>LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table </li></ul></ul><ul><ul><li>RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table </li></ul></ul><ul><ul><li>FULL JOIN: Return rows when there is a match in one of the tables </li></ul></ul>
  27. 27. 1) Join SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name Eg: The &quot;Persons&quot; table: P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger The &quot;Orders&quot; table: O_Id OrderNo P_Id 1 77895 3 2 44678 3 3 22456 1 4 24562 1 5 34764 15
  28. 28. SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.P_Id=Orders.P_Id; LastName FirstName OrderNo Hansen Ola 22456 Hansen Ola 24562 Pettersen Kari 77895 Pettersen Kari 44678 2) Left join SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name eg: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id
  29. 29. LastName FirstName OrderNo Hansen Ola 22456 Hansen Ola 24562 Pettersen Kari 77895 Pettersen Kari 44678 Svendson Tove 3) Right join SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name eg: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.P_Id=Orders.P_Id
  30. 30. LastName FirstName OrderNo Hansen Ola 22456 Hansen Ola 24562 Pettersen Kari 77895 Pettersen Kari 44678 34764 4) Full join SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name eg: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons FULL JOIN Orders ON Persons.P_Id=Orders.P_Id
  31. 31. LastName FirstName OrderNo Hansen Ola 22456 Hansen Ola 24562 Pettersen Kari 77895 Pettersen Kari 44678 Svendson Tove 34764

×