Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. SQL – Structured Query Language By Pratik Rathod
  2. 2. DATABASE• Organized collection of data• DBMS – Data Base Management System• Ex. Oracle, Microsoft SQL Server, IBM DB2• 2 types of DBMS – Flat-file database – Relational database
  3. 3. SQL• Structured Query Language -> RDBMS• A little History – Developed by Donald D. Chamberlin & Raymond F. Boyce at IBM in 1970 – Initially it was named as SEQUEL (Structured English Query Language)• Queries – most commonly used operation & can be done by SELECT• SQL is not case sensitive. 
  4. 4. SQL for…• Execute queries against database• Retrieve data from database• Insert, Update, Delete records in database• Create new tables in database• Set permissions on tables• “Persons” table in database ID Firstname Lastname Address City 1Pratik Rathod memnagar Ahmedabad 2Pranav Agarwal Paldi Ahmedabad 3Karan Koria Girnar Junagadh
  5. 5. SQL Statements• Not case sensitive• Semi columns• SQL - DML – Data manipulation language SQL - DDL - Data definition language DML DDL CREATE SELECT DATABASE UPDATE ALTER DATABASE DELETE CREATE TABLE INSERT INTO ALTER TABLE DROP TBALE CREATE INDEX DROP INDEX
  6. 6. SQL Syntax & Statements• SELECTSyntax : SELECT column_name(s) FROM table_nameExamples:1) SELECT LastName,FirstName FROM Persons2) SELECT * FROM Persons (* is a quick way to select all the columns)Where our selected columns stored????... ID Firstname Lastname Address City 1Pratik Rathod memnagar Ahmedabad 2Pranav Agarwal Paldi Ahmedabad 3Karan Koria Girnar Junagadh
  7. 7. SQL Syntax & Statements• SELECT DISTINCT • Where (clause)Syntax : Syntax :SELECT DISTINCT column_name(s) SELECT column_name(s) FROM table_nameFROM table_name WHERE column_name operator valueExample: Example:SELECT DISTINCT City FROM Persons SELECT * FROM Persons WHERE City=‘Ahmedabad‘CityAhmedabad ID Firstname Lastname Address CityJunagadh 1Pratik Rathod memnagar Ahmedabad 2Pranav Agarwal Paldi Ahmedabad
  8. 8. SQL Syntax & Statements• Operators in WHERE • CREATE TABLE = Equals Syntax : <> or != Not equal CREATE TABLE table_name > Greater than ( < Less than column_name1 data_type, column_name2 data_type, >= Greater than or equal column_name3 data_type, <= Less than or equal .... ) Example: CREATE TABLE Persons ( Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )
  9. 9. SQL Syntax & Statements• AND (Operator) • AND & OR togetherEx. Ex.SELECT * FROM Persons SELECT * FROM Persons WHEREWHERE FirstName=‘Pranav‘ AND LastName=‘Agarwal LastName=‘Agarwal‘ AND (FirstName=‘Pranav OR FirstName=‘Pratik)• OR (Operator)Ex.SELECT * FROM PersonsWHERE FirstName=‘Pranav OR FirstName=‘Pratik
  10. 10. SQL Syntax & Statements• ORDER BY (Clause) • INSERT INTOSyntax : SELECT column_name(s) FROM table_name – To insert a new row ORDER BY column_name(s) ASC|DESC Syntax : INSERT INTO table_name (column1, column2, column3,...)Ex. VALUES (value1, value2, value3,...)1) SELECT * FROM Persons Ex. ORDER BY LastName 1) INSERT INTO Persons2) SELECT * FROM Persons VALUES ORDER BY LastName DESC (4,‘Naman, ‘Shah, ‘gandhinagar, ‘g andhinagar) 2) INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, ‘Amin, ‘Jay)
  11. 11. SQL Syntax & Statements• UPDATE • DELETESyntax : UPDATE table_name Syntax : DELETE FROM table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example:Ex. 1) DELETE * FROM Persons 2) DELETE FROM PersonsUPDATE Persons WHERE Lastname = ‘Rathod’SET Address=‘Sector21,City=‘Gandhinagar WHERE LastName=‘Shah AND FirstName=‘Naman’ CAUTION: DELETE cannot be undo.
  12. 12. SQL elements
  13. 13. Where to write this things??...• Install java platform with JDBC API• Install a driver• Install DBMS (if u don’t have any)• Set up Database• Load drivers (In java program)• Make connection (In java program)• Write SQL statements in your program. – E.g. Statement stmt = con.createStatement(); ( stmt.executeUpdate "INSERT INTO COFFEES " + "VALUES (Colombian, 101, 7.99, 0, 0)");
  14. 14. Thank you