Your SlideShare is downloading. ×
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS

872

Published on

SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data in a database. …

SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data in a database. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc.

NewyorkSys is one of the leading top Training and Consulting Company in US. Newyorksys have certified trainers. We will provide Online Training, Fast Track online training, with job assistance. We are providing excellent Training in all courses.

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
872
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
133
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ORACLE PL/SQL TUTORIALSNewyorkSys is one of the leading top Training and ConsultingCompany in US. Newyorksys have certified trainers. We will provideOnline Training, Fast Track online training, with job assistance. Weare providing excellent Training in all courses. Faculty from topMNC’s with highly skilled domain expertise will train & guide you withreal time examples, project explanation. We also help you in resumepreparation and provide job assistance till you get job.Oracle• Sql/ PL Sql• 10g / 11g DBA• Apps DBA• Apps CRM• Apps Framework• Apps HRMS• Apps Technical• ADF• SOA / BPEL• OBIEEUSA: +1- 718- 313- 0499 USA:+1- 718- 305- 1757E-mail ID: enquiry@newyorksys.usVisit: www.NewyorkSys.com
  • 2. Structured Query LanguageSQL is a standard computer language for accessing and manipulating databases.What is SQL?• SQL stands for Structured Query Language• SQL allows you to access a database• SQL is an ANSI standard computer language• SQL can execute queries against a database• SQL can retrieve data from a database• SQL can insert new records in a database• SQL can delete records from a database• SQL can update records in a database• SQL is easy to learnSQL is a Standard - BUT....SQL is an ANSI (American National Standards Institute) standardcomputer language for accessing and manipulating database systems.SQL statements are used to retrieve and update data in a database. SQLworks with database programs like MS Access, DB2, Informix, MS SQLServer, Oracle, Sybase, etc.Unfortunately, there are many different versions of the SQL language,but to be in compliance with the ANSI standard, they must support thesame major keywords in a similar manner (such as SELECT, UPDATE,DELETE, INSERT, WHERE, and others).Note: Most of the SQL database programs also have their ownproprietary extensions in addition to the SQL standard!SQL Database TablesA database most often contains one or more tables. Each table isidentified by a name (e.g. "Customers" or "Orders"). Tables containrecords (rows) with data.Below is an example of a table called "Persons":LastName FirstName Address CityHansen Ola Timoteivn 10 SandnesSvendson Tove Borgvn 23 SandnesPettersen Kari Storgt 20 Stavanger
  • 3. The table above contains three records (one for each person) and fourcolumns (LastName, FirstName, Address, and City).SQL QueriesWith SQL, we can query a database and have a result set returned.A query like this:SELECT LastName FROM PersonsGives a result set like this:LastNameHansenSvendsonPettersenNote: Some database systems require a semicolon at the end of the SQLstatement. We dont use the semicolon in our tutorials.SQL Data Manipulation Language (DML)SQL (Structured Query Language) is a syntax for executing queries. Butthe SQL language also includes a syntax to update, insert, and deleterecords.These query and update commands together form the Data ManipulationLanguage (DML) part of SQL: SELECT - extracts data from a database table UPDATE - updates data in a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into a database tableSQL Data Definition Language (DDL)The Data Definition Language (DDL) part of SQL permits database tablesto be created or deleted. We can also define indexes (keys), specify linksbetween tables, and impose constraints between database tables.The most important DDL statements in SQL are: CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table
  • 4.  CREATE INDEX - creates an index (search key) DROP INDEX - deletes an indexThe SELECT StatementThe SELECT statement is used to select data from a table. The tabularresult is stored in a result table (called the result-set).SyntaxSELECT column_name(s)FROM table_nameSelect Some ColumnsTo select the columns named "LastName" and "FirstName", use aSELECT statement like this:SELECT LastName,FirstName FROM Persons"Persons" tableLastName FirstName Address CityHansen Ola Timoteivn 10 SandnesSvendson Tove Borgvn 23 SandnesPettersen Kari Storgt 20 StavangerResultLastName FirstNameHansen OlaSvendson TovePettersen KariSelect All ColumnsTo select all columns from the "Persons" table, use a * symbol instead ofcolumn names, like this:SELECT * FROM Persons
  • 5. ResultLastName FirstName Address CityHansen Ola Timoteivn 10 SandnesSvendson Tove Borgvn 23 SandnesPettersen Kari Storgt 20 StavangerThe Result SetThe result from a SQL query is stored in a result-set. Most databasesoftware systems allow navigation of the result set with programmingfunctions, like: Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, etc.Programming functions like these are not a part of this tutorial. To learnabout accessing data with function calls, please visit our ADO tutorial.Semicolon after SQL Statements?Semicolon is the standard way to separate each SQL statement indatabase systems that allow more than one SQL statement to beexecuted in the same call to the server.Some SQL tutorials end each SQL statement with a semicolon. Is thisnecessary? We are using MS Access and SQL Server 2000 and we do nothave to put a semicolon after each SQL statement, but some databaseprograms force you to use it.The SELECT DISTINCT StatementThe DISTINCT keyword is used to return only distinct (different) values.The SELECT statement returns information from table columns. But whatif we only want to select distinct elements?With SQL, all we need to do is to add a DISTINCT keyword to the SELECTstatement:SyntaxSELECT DISTINCT column_name(s)
  • 6. FROM table_nameUsing the DISTINCT keywordTo select ALL values from the column named "Company" we use aSELECT statement like this:SELECT Company FROM Orders"Orders" tableCompany OrderNumberSega 3412W3Schools 2312Trio 4678W3Schools 6798ResultCompanySegaW3SchoolsTrioW3SchoolsNote that "W3Schools" is listed twice in the result-set.To select only DIFFERENT values from the column named "Company" weuse a SELECT DISTINCT statement like this:SELECT DISTINCT Company FROM OrdersResult:CompanySegaW3SchoolsTrioThe WHERE clause is used to specify a selection criterion.
  • 7. The WHERE ClauseTo conditionally select data from a table, a WHERE clause can be addedto the SELECT statement.SyntaxSELECT column FROM tableWHERE column operator valueWith the WHERE clause, the following operators can be used:Operator Description= Equal<> Not equal> Greater than< Less than>= Greater than or equal<= Less than or equalBETWEEN Between an inclusive rangeLIKESearch for a patternNote: In some versions of SQL the <> operator may be written as !=Using the WHERE ClauseTo select only the persons living in the city "Sandnes", we add a WHEREclause to the SELECT statement:SELECT * FROM PersonsWHERE City=Sandnes"Persons" tableLastName FirstName Address City YearHansen Ola Timoteivn 10 Sandnes 1951Svendson Tove Borgvn 23 Sandnes 1978Svendson Stale Kaivn 18 Sandnes 1980Pettersen Kari Storgt 20 Stavanger 1960
  • 8. ResultLastName FirstName Address City YearHansen Ola Timoteivn 10 Sandnes 1951Svendson Tove Borgvn 23 Sandnes 1978Svendson Stale Kaivn 18 Sandnes 1980Using QuotesNote that we have used single quotes around the conditional values inthe examples.SQL uses single quotes around text values (most database systems willalso accept double quotes). Numeric values should not be enclosed inquotes.For text values:This is correct:SELECT * FROM Persons WHERE FirstName=ToveThis is wrong:SELECT * FROM Persons WHERE FirstName=ToveFor numeric values:This is correct:SELECT * FROM Persons WHERE Year>1965This is wrong:SELECT * FROM Persons WHERE Year>1965The LIKE ConditionThe LIKE condition is used to specify a search for a pattern in a column.SyntaxSELECT column FROM tableWHERE column LIKE patternA "%" sign can be used to define wildcards (missing letters in thepattern) both before and after the pattern.Using LIKE
  • 9. The following SQL statement will return persons with first names thatstart with an O:SELECT * FROM PersonsWHERE FirstName LIKE O%The following SQL statement will return persons with first names thatend with an a:SELECT * FROM PersonsWHERE FirstName LIKE %aThe following SQL statement will return persons with first names thatcontain the pattern la:SELECT * FROM PersonsWHERE FirstName LIKE %la%The INSERT INTO StatementThe INSERT INTO statement is used to insert new rows into a table.SyntaxINSERT INTO table_nameVALUES (value1, value2,....)You can also specify the columns for which you want to insert data:INSERT INTO table_name (column1, column2,...)VALUES (value1, value2,....)Insert a New RowThis "Persons" table:LastName FirstName Address CityPettersen Kari Storgt 20 StavangerAnd this SQL statement:INSERT INTO PersonsVALUES (Hetland, Camilla, Hagabakka 24, Sandnes)
  • 10. Will give this result:LastName FirstName Address CityPettersen Kari Storgt 20 StavangerHetland Camilla Hagabakka 24 SandnesInsert Data in Specified ColumnsThis "Persons" table:LastName FirstName Address CityPettersen Kari Storgt 20 StavangerHetland Camilla Hagabakka 24 SandnesAnd This SQL statement:INSERT INTO Persons (LastName, Address)VALUES (Rasmussen, Storgt 67)Will give this result:LastName FirstName Address CityPettersen Kari Storgt 20 StavangerHetland Camilla Hagabakka 24 SandnesRasmussen Storgt 67The Update StatementThe UPDATE statement is used to modify the data in a table.SyntaxUPDATE table_nameSET column_name = new_valueWHERE column_name = some_valuePerson:LastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Storgt 67
  • 11. Update one Column in a RowWe want to add a first name to the person with a last name of"Rasmussen":UPDATE Person SET FirstName = NinaWHERE LastName = RasmussenResult:LastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Nina Storgt 67Update several Columns in a RowWe want to change the address and add the name of the city:UPDATE PersonSET Address = Stien 12, City = StavangerWHERE LastName = RasmussenResult:LastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Nina Stien 12 StavangerThe Delete StatementThe DELETE statement is used to delete rows in a table.SyntaxDELETE FROM table_nameWHERE column_name = some_valuePerson:LastName FirstName Address City
  • 12. Nilsen Fred Kirkegt 56 StavangerRasmussen Nina Stien 12 StavangerDelete a Row"Nina Rasmussen" is going to be deleted:DELETE FROM Person WHERE LastName = RasmussenResultLastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerDelete All RowsIt is possible to delete all rows in a table without deleting the table. Thismeans that the table structure, attributes, and indexes will be intact:DELETE FROM table_nameorDELETE * FROM table_nameSQL SyntaxStatement SyntaxAND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR conditionALTER TABLE (addcolumn)ALTER TABLE table_nameADD column_name datatypeALTER TABLE (dropcolumn)ALTER TABLE table_nameDROP COLUMN column_nameAS (alias for column) SELECT column_name AS column_aliasFROM table_name+AS (alias for table) SELECT column_nameFROM table_name AS table_alias
  • 13. BETWEEN SELECT column_name(s)FROM table_nameWHERE column_nameBETWEEN value1 AND value2CREATE DATABASE CREATE DATABASE database_nameCREATE INDEX CREATE INDEX index_nameON table_name (column_name)CREATE TABLE CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,.......)CREATE UNIQUE INDEX CREATE UNIQUE INDEX index_nameON table_name (column_name)CREATE VIEW CREATE VIEW view_name ASSELECT column_name(s)FROM table_nameWHERE conditionDELETE FROM DELETE FROM table_name(Note: Deletes the entire table!!) (Not thestructure)orDELETE FROM table_nameWHERE conditionDROP DATABASE DROP DATABASE database_nameDROP INDEX DROP INDEX table_name.index_nameDROP TABLE DROP TABLE table_nameGROUP BY SELECT column_name1,SUM(column_name2)FROM table_nameGROUP BY column_name1HAVING SELECT column_name1,SUM(column_name2)FROM table_nameGROUP BY column_name1HAVING SUM(column_name2) condition valueIN SELECT column_name(s)FROM table_nameWHERE column_nameIN (value1,value2,..)INSERT INTO INSERT INTO table_nameVALUES (value1, value2,....)
  • 14. orINSERT INTO table_name(column_name1, column_name2,...)VALUES (value1, value2,....)LIKE SELECT column_name(s)FROM table_nameWHERE column_nameLIKE patternORDER BY SELECT column_name(s)FROM table_nameORDER BY column_name [ASC|DESC]SELECT SELECT column_name(s)FROM table_nameSELECT * SELECT *FROM table_nameSELECT DISTINCT SELECT DISTINCT column_name(s)FROM table_nameSELECT INTO(used to create backupcopies of tables)SELECT *INTO new_table_nameFROM original_table_nameorSELECT column_name(s)INTO new_table_nameFROM original_table_nameTRUNCATE TABLE(deletes only the datainside the table)TRUNCATE TABLE table_nameUPDATE UPDATE table_nameSET column_name=new_value[, column_name=new_value]WHERE column_name=some_valueWHERE SELECT column_name(s)FROM table_nameWHERE conditionBETWEEN ... ANDThe BETWEEN ... AND operator selects a range of data between twovalues. These values can be numbers, text, or dates.SELECT column_name FROM table_nameWHERE column_name
  • 15. BETWEEN value1 AND value2Original Table (used in the examples)LastName FirstName Address CityHansen Ola Timoteivn 10 SandnesNordmann Anna Neset 18 SandnesPettersen Kari Storgt 20 StavangerSvendson Tove Borgvn 23 SandnesExample 1To display the persons alphabetically between (and including) "Hansen"and exclusive "Pettersen", use the following SQL:SELECT * FROM Persons WHERE LastNameBETWEEN Hansen AND PettersenResult:LastName FirstName Address CityHansen Ola Timoteivn 10 SandnesNordmann Anna Neset 18 SandnesIMPORTANT! The BETWEEN...AND operator is treated differently indifferent databases. With some databases a person with the LastNameof "Hansen" or "Pettersen" will not be listed (BETWEEN..AND onlyselects fields that are between and excluding the test values). Withsome databases a person with the last name of "Hansen" or "Pettersen"will be listed (BETWEEN..AND selects fields that are between andincluding the test values). With other databases a person with the lastname of "Hansen" will be listed, but "Pettersen" will not be listed(BETWEEN..AND selects fields between the test values, including thefirst test value and excluding the last test value). Therefore: Check howyour database treats the BETWEEN....AND operator!Example 2To display the persons outside the range used in the previous example,use the NOT operator:SELECT * FROM Persons WHERE LastNameNOT BETWEEN Hansen AND Pettersen
  • 16. Result:LastName FirstName Address CityPettersen Kari Storgt 20 StavangerSvendson Tove Borgvn 23 SandnesThe ORDER BY keyword is used to sort the result.Sort the RowsThe ORDER BY clause is used to sort the rows.Orders:Company OrderNumberSega 3412ABC Shop 5678W3Schools 2312W3Schools 6798ExampleTo display the companies in alphabetical order:SELECT Company, OrderNumber FROM OrdersORDER BY CompanyResult:Company OrderNumberABC Shop 5678Sega 3412W3Schools 6798W3Schools 2312ExampleTo display the companies in alphabetical order AND the ordernumbers innumerical order:SELECT Company, OrderNumber FROM Orders
  • 17. ORDER BY Company, OrderNumberResult:Company OrderNumberABC Shop 5678Sega 3412W3Schools 2312W3Schools 6798ExampleTo display the companies in reverse alphabetical order:SELECT Company, OrderNumber FROM OrdersORDER BY Company DESCResult:Company OrderNumberW3Schools 6798W3Schools 2312Sega 3412ABC Shop 5678ExampleTo display the companies in reverse alphabetical order AND theordernumbers in numerical order:SELECT Company, OrderNumber FROM OrdersORDER BY Company DESC, OrderNumber ASCResult:Company OrderNumberW3Schools 2312W3Schools 6798Sega 3412ABC Shop 5678IN
  • 18. The IN operator may be used if you know the exact value you want toreturn for at least one of the columns.SELECT column_name FROM table_nameWHERE column_name IN (value1,value2,..)Original Table (used in the examples)LastName FirstName Address CityHansen Ola Timoteivn 10 SandnesNordmann Anna Neset 18 SandnesPettersen Kari Storgt 20 StavangerSvendson Tove Borgvn 23 SandnesExample 1To display the persons with LastName equal to "Hansen" or "Pettersen",use the following SQL:SELECT * FROM PersonsWHERE LastName IN (Hansen,Pettersen)Result:LastName FirstName Address CityHansen Ola Timoteivn 10 SandnesPettersen Kari Storgt 20 StavangerAND & ORAND and OR join two or more conditions in a WHERE clause.The AND operator displays a row if ALL conditions listed are true. TheOR operator displays a row if ANY of the conditions listed are true.Original Table (used in the examples)LastName FirstName Address CityHansen Ola Timoteivn 10 SandnesSvendson Tove Borgvn 23 SandnesSvendson Stephen Kaivn 18 Sandnes
  • 19. ExampleUse AND to display each person with the first name equal to "Tove", andthe last name equal to "Svendson":SELECT * FROM PersonsWHERE FirstName=ToveAND LastName=SvendsonResult:LastName FirstName Address CitySvendson Tove Borgvn 23 SandnesExampleUse OR to display each person with the first name equal to "Tove", orthe last name equal to "Svendson":SELECT * FROM PersonsWHERE firstname=ToveOR lastname=SvendsonResult:LastName FirstName Address CitySvendson Tove Borgvn 23 SandnesSvendson Stephen Kaivn 18 SandnesExampleYou can also combine AND and OR (use parentheses to form complexexpressions):SELECT * FROM Persons WHERE(FirstName=Tove OR FirstName=Stephen)AND LastName=SvendsonResult:LastName FirstName Address CitySvendson Tove Borgvn 23 SandnesSvendson Stephen Kaivn 18 Sandnes
  • 20. Aggregate functions (like SUM) often need an added GROUP BY functionality.GROUP BY...GROUP BY... was added to SQL because aggregate functions (like SUM)return the aggregate of all column values every time they are called, andwithout the GROUP BY function it was impossible to find the sum foreach individual group of column values.The syntax for the GROUP BY function is:SELECT column,SUM(column) FROM table GROUP BY columnGROUP BY ExampleThis "Sales" Table:Company AmountW3Schools 5500IBM 4500W3Schools 7100And This SQL:SELECT Company, SUM(Amount) FROM SalesReturns this result:Company SUM(Amount)W3Schools 17100IBM 17100W3Schools 17100The above code is invalid because the column returned is not part of anaggregate. A GROUP BY clause will solve this problem:SELECT Company,SUM(Amount) FROM SalesGROUP BY Company
  • 21. Returns this result:Company SUM(Amount)W3Schools 12600IBM 4500HAVING...HAVING... was added to SQL because the WHERE keyword could not beused against aggregate functions (like SUM), and without HAVING... itwould be impossible to test for result conditions.The syntax for the HAVING function is:SELECT column,SUM(column) FROM tableGROUP BY columnHAVING SUM(column) condition valueThis "Sales" Table:Company AmountW3Schools 5500IBM 4500W3Schools 7100This SQL:SELECT Company,SUM(Amount) FROM SalesGROUP BY CompanyHAVING SUM(Amount)>10000Returns this resultCompany SUM(Amount)W3Schools 12600The SELECT INTO Statement
  • 22. The SELECT INTO statement is most often used to create backup copiesof tables or for archiving records.SyntaxSELECT column_name(s) INTO newtable [IN externaldatabase]FROM sourceMake a Backup CopyThe following example makes a backup copy of the "Persons" table:SELECT * INTO Persons_backupFROM PersonsThe IN clause can be used to copy tables into another database:SELECT Persons.* INTO Persons IN Backup.mdbFROM PersonsIf you only want to copy a few fields, you can do so by listing them afterthe SELECT statement:SELECT LastName,FirstName INTO Persons_backupFROM PersonsYou can also add a WHERE clause. The following example creates a"Persons_backup" table with two columns (FirstName and LastName)by extracting the persons who lives in "Sandnes" from the "Persons"table:SELECT LastName,Firstname INTO Persons_backupFROM PersonsWHERE City=SandnesSelecting data from more than one table is also possible. The followingexample creates a new table "Empl_Ord_backup" that contains datafrom the two tables Employees and Orders:SELECT Employees.Name,Orders.ProductINTO Empl_Ord_backupFROM EmployeesINNER JOIN OrdersON Employees.Employee_ID=Orders.Employee_ID
  • 23. ALTER TABLEThe ALTER TABLE statement is used to add or drop columns in anexisting table.ALTER TABLE table_nameADD column_name datatypeALTER TABLE table_nameDROP COLUMN column_nameNote: Some database systems dont allow the dropping of a column in adatabase table (DROP COLUMN column_name).Person:LastName FirstName AddressPettersen Kari Storgt 20ExampleTo add a column named "City" in the "Person" table:ALTER TABLE Person ADD City varchar(30)Result:LastName FirstName Address CityPettersen Kari Storgt 20ExampleTo drop the "Address" column in the "Person" table:ALTER TABLE Person DROP COLUMN AddressResult:LastName FirstName CityPettersen KariSQL has a lot of built-in functions for counting and calculations.
  • 24. Function SyntaxThe syntax for built-in SQL functions is:SELECT function(column) FROM tableTypes of FunctionsThere are several basic types and categories of functions in SQL. Thebasic types of functions are:• Aggregate Functions• Scalar functionsAggregate functionsAggregate functions operate against a collection of values, but return asingle value.Note: If used among many other expressions in the item list of a SELECTstatement, the SELECT must have a GROUP BY clause!!"Persons" table (used in most examples)Name AgeHansen, Ola 34Svendson, Tove 45Pettersen, Kari 19Aggregate functions in MS AccessFunction DescriptionAVG(column) Returns the average value of a columnCOUNT(column) Returns the number of rows (without a NULLvalue) of a columnCOUNT(*) Returns the number of selected rowsFIRST(column) Returns the value of the first record in thespecified fieldLAST(column) Returns the value of the last record in thespecified field
  • 25. MAX(column) Returns the highest value of a columnMIN(column) Returns the lowest value of a columnSTDEV(column)STDEVP(column)SUM(column) Returns the total sum of a columnVAR(column)VARP(column)Aggregate functions in SQL ServerFunction DescriptionAVG(column) Returns the average value of a columnBINARY_CHECKSUMCHECKSUMCHECKSUM_AGGCOUNT(column) Returns the number of rows (without a NULLvalue) of a columnCOUNT(*) Returns the number of selected rowsCOUNT(DISTINCTcolumn)Returns the number of distinct resultsFIRST(column) Returns the value of the first record in thespecified field (not supported in SQLServer2K)LAST(column) Returns the value of the last record in thespecified field (not supported in SQLServer2K)MAX(column) Returns the highest value of a columnMIN(column) Returns the lowest value of a columnSTDEV(column)STDEVP(column)SUM(column) Returns the total sum of a columnVAR(column)VARP(column)Scalar functionsScalar functions operate against a single value, and return a single valuebased on the input value.Useful Scalar Functions in MS Access
  • 26. Function DescriptionUCASE(c) Converts a field to upper caseLCASE(c) Converts a field to lower caseMID(c,start[,end]) Extract characters from a text fieldLEN(c) Returns the length of a text fieldINSTR(c) Returns the numeric position of a namedcharacter within a text fieldLEFT(c,number_of_char) Return the left part of a text field requestedRIGHT(c,number_of_char) Return the right part of a text field requestedROUND(c,decimals) Rounds a numeric field to the number ofdecimals specifiedMOD(x,y) Returns the remainder of a division operationNOW() Returns the current system dateFORMAT(c,format) Changes the way a field is displayedDATEDIFF(d,date1,date2) Used to perform date calculationsUNIONThe UNION command is used to select related information from twotables, much like the JOIN command. However, when using the UNIONcommand all selected columns need to be of the same data type.Note: With UNION, only distinct values are selected.SQL Statement 1UNIONSQL Statement 2Employees_Norway:Employee_ID E_Name01 Hansen, Ola02 Svendson, Tove03 Svendson, Stephen04 Pettersen, KariEmployees_USA:Employee_ID E_Name
  • 27. 01 Turner, Sally02 Kent, Clark03 Svendson, Stephen04 Scott, StephenUsing the UNION CommandExampleList all different employee names in Norway and USA:SELECT E_Name FROM Employees_NorwayUNIONSELECT E_Name FROM Employees_USAResultNameHansen, OlaSvendson, ToveSvendson, StephenPettersen, KariTurner, SallyKent, ClarkScott, StephenNote: This command cannot be used to list all employees in Norway andUSA. In the example above we have two employees with equal names,and only one of them is listed. The UNION command only selects distinctvalues.UNION ALLThe UNION ALL command is equal to the UNION command, except thatUNION ALL selects all values.SQL Statement 1UNION ALLSQL Statement 2Using the UNION ALL Command
  • 28. ExampleList all employees in Norway and USA:SELECT E_Name FROM Employees_NorwayUNION ALLSELECT E_Name FROM Employees_USAResultNameHansen, OlaSvendson, ToveSvendson, StephenPettersen, KariTurner, SallyKent, ClarkSvendson, StephenScott, Stephen
  • 29. NewyorkSys is one of the leading top Training and ConsultingCompany in US. Newyorksys have certified trainers. We will provideOnline Training, Fast Track online training, with job assistance. Weare providing excellent Training in all courses. Faculty from topMNC’s with highly skilled domain expertise will train & guide you withreal time examples, project explanation. We also help you in resumepreparation and provide job assistance till you get job.Oracle• Sql/ PL Sql• 10g / 11g DBA• Apps DBA• Apps CRM• Apps Framework• Apps HRMS• Apps Technical• ADF• SOA / BPEL• OBIEEFor additional assistance, including Course details, Batch schedulinginformation, Mail us or you may contact to below address:CONTACT USNewyorkSys Inc.,15 Roaring Brook Rd, Chappaqua, NY 10514.USA: +1-718-313-0499 USA:+1- 718-305-1757E-mail ID: enquiry@newyorksys.usVisit: www.NewyorkSys.com

×