27
BIL101, Introduction to BIL101, Introduction to Computers and Computers and Information Systems Information Systems Chapter 11 Chapter 11 Sample SQL Applications Sample SQL Applications Prepared by Prepared by Metin Demiralp Metin Demiralp Istanbul Technical University, Informatics Istanbul Technical University, Informatics Institute, Institute, Maslak { 80626, Istanbul, Türkiye) Maslak { 80626, Istanbul, Türkiye) Version 0. 60 Version 0. 60 http:// http:// www.be.itu.edu.tr www.be.itu.edu.tr

BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

BIL101, Introduction to Computers  and  BIL101, Introduction to Computers  and  Information Systems Information Systems                   Chapter 11                  Chapter 11

Sample SQL ApplicationsSample SQL ApplicationsPrepared byPrepared by

Metin DemiralpMetin DemiralpIstanbul Technical University, Informatics Institute,Istanbul Technical University, Informatics Institute,

Maslak { 80626, Istanbul, Türkiye)Maslak { 80626, Istanbul, Türkiye)Version 0. 60Version 0. 60

http://www.be.itu.edu.trhttp://www.be.itu.edu.tr

Page 2: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

Sample SQL ApplicationsSample SQL Applications

This chapter is mainly devoted to some applications This chapter is mainly devoted to some applications of SQL. There are various sample applications in this of SQL. There are various sample applications in this notes to explain and illustrate how SQL works. You notes to explain and illustrate how SQL works. You can use interface at the URL can use interface at the URL

http://www.be.itu.edu.tr/bedershttp://www.be.itu.edu.tr/beders or or

http://www.sqlcourse2.comhttp://www.sqlcourse2.com

It is possible to run many types of SQL applications It is possible to run many types of SQL applications and see the output through this interface.and see the output through this interface.

Page 3: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

QueriesQueries As you know SQL is an acronym for the statement As you know SQL is an acronym for the statement

Structured Query LanguageStructured Query Language. However, SQL does a . However, SQL does a lot of things which are not included in the meaning of lot of things which are not included in the meaning of the term the term QueryQuery. .

It creates, deletes, modifies, joins data and more. It creates, deletes, modifies, joins data and more. The data in a database is case sensitive while the SQL The data in a database is case sensitive while the SQL

commands are case insensitive. This means that you commands are case insensitive. This means that you can write SQL commands either in lowercase or can write SQL commands either in lowercase or uppercase.uppercase.

Page 4: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

The spacing is not important. That is, you can put The spacing is not important. That is, you can put spaces between the words as much as you want and spaces between the words as much as you want and you can give a command in a single or more than one you can give a command in a single or more than one lines at the SQL prompt.lines at the SQL prompt.

SELECT command is one of the keywords which SELECT command is one of the keywords which take important roles in SQL syntax and hence it is take important roles in SQL syntax and hence it is preferred to capitalize this command although preferred to capitalize this command although nothing is wrong if it is given in lowercase letters.nothing is wrong if it is given in lowercase letters.

Page 5: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

SELECT can not be used alone. It needs some parameters SELECT can not be used alone. It needs some parameters or other keywords. or other keywords.

Therefore, if you just enter Therefore, if you just enter SELECT;SELECT; at the sql prompt it will at the sql prompt it will fail to work and the SQL program will complain by fail to work and the SQL program will complain by announcing a missing expression. Under the simplest announcing a missing expression. Under the simplest conditions the column name of the table under conditions the column name of the table under consideration must be specified after SELECT. However, consideration must be specified after SELECT. However, this is not sufficient because the table name has not been this is not sufficient because the table name has not been specified. specified.

select "column1"[,"column2",etc] from "tablename” [where "condition"];

[] = optional

Page 6: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

This specification can be done by using the keyword This specification can be done by using the keyword FROM which must be followed by the name of the FROM which must be followed by the name of the table under consideration. table under consideration.

Three important items under SQL are keyword, Three important items under SQL are keyword, clause, and statement. The keyword refer to an clause, and statement. The keyword refer to an individual SQL element like SELECT and FROM. individual SQL element like SELECT and FROM. A clause is a part of an SQL statement. For A clause is a part of an SQL statement. For example, example, SELECT column1, column2,.......SELECT column1, column2,....... is a is a clause. A statement is a combination of SQL clause. A statement is a combination of SQL clauses. For example, you can combine a SELECT clauses. For example, you can combine a SELECT and FROM clause to write an SQL statement. and FROM clause to write an SQL statement.

The column names that follow the select keyword determine which columns will be returned in the results. You can select as many column names that you'd like, or you can use a "*" to select all columns.

Page 7: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

We can apply the following query command to the STUDENTS table. We can apply the following query command to the STUDENTS table.

select * from students;select * from students; This will produce the following output This will produce the following output through the interface we use here.through the interface we use here.

Register_No Name Surname Faculty Program Exam

09010078 Ahmet Yolcu Arts and Sciences Mathematics 85

04039923 Nazlı Koşan Electronics Comp. Eng. 78

04039923 Umut Karabasan Electronics Comp. Eng. 63

01010065 Fatih Koklayan Construction Const. Eng. 45

02010029 Süzen Kaşıkara Architecture Architecture 89

06019809 Bulut Yağan Chem.--Met. Chem. Eng. 75

07029714 Yağmur Islatan Mngmnt. Eng Ind. Eng. 65

StudentsStudents

Page 8: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

select register_no, faculty, program, name, surname from students;

results in the following display. 

Register_NoFaculty Program Name Surname

09010078Arts and Sciences

Mathematics Ahmet Yolcu

04039923 Electronics Comp. Eng. Nazlı Koşan

04039923 Electronics Comp. Eng. Umut Karabasan

01010065 Construction Const. Eng. Fatih Koklayan

02010029 Architecture Architecture Süzen Kaşıkara

06019809 Chem.--Met. Chem. Eng. Bulut Yağan

07029714Mngmnt. Eng

Ind. Eng. Yağmur Islatan

Page 9: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

the faculty, Electronics, is repeated. However it is possible to get rid of the repetitions in the display. For example the following example removes repetitions in the display. select distinct faculty from students;The resulting display is as below.

Faculty

Arts and Sciences

Electronics

Construction

Architecture

Chem.--Met.

Mngmnt. Eng

Therefore the key distinct is used for the removal of the multiplicated data

Page 10: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

The arithmetic operators are plus (+), minus (-), divide (/), multiply (*), and modulo (\). The first four are traditional elementary arithmetic operators. The modulo operator returns the integer remainder of a division. We can explain this by giving the following examples. 7 / 2 = 18 / 2 = 08 / 3 = 29 / 3 = 0   The modulo operator does work only with integer data type. 

Page 11: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

If several of these arithmetic operators are placed in an expression without any parentheses, the operators are resolved with this order: multiplication, division, modulo, addition, and subtraction. For example, the expression3 * 7 + 12/4 equals 21 + 3 = 24However, the expression3 * (7 + 12) / 4 equals 3 * 19 / 4 = 14   Therefore you have to watch where you put parentheses in an expression. Sometimes the expression does exactly what you instruct it to do, rather than what you want it to do.

Page 12: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

Now we deal with the multiplication operator in the queries. Let us start by creating a prices list in the database table PRICES whose display through the command select * from prices is given below.

Item Price

Tomato 300000

Grape 850000

Tangerine 500000

Pomegranate 1200000

Quince 700000

Chestnut 1300000

Apple 500000

Page 13: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

select item, price, price*1.2 from prices; then we obtain the following display.

Item Price Price*1.2

Tomato 300000 360000

Grape 850000 1020000

Tangerine 500000 600000

Pomegranate 1200000 1440000

Quince 700000 840000

Chestnut 1300000 1560000

Apple 500000 600000   

Page 14: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

SQL FunctionsSQL Functions

SQL can use some functions to realize certain SQL can use some functions to realize certain predefined actions. A function is composed of predefined actions. A function is composed of three things: three things:

Name, Action, Name, Action, andand Argument Argument. .

Argument is mostly a columnname. Argument is mostly a columnname.

Therefore, we can symbolically denote a Therefore, we can symbolically denote a function as function as functionname (columnname).functionname (columnname).

Page 15: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

SQL functions can be categorized into classes which SQL functions can be categorized into classes which can be called aggregated functions, data and time can be called aggregated functions, data and time functions, arithmetic functions, character functions, functions, arithmetic functions, character functions, conversion functions, and miscellaneous functions. conversion functions, and miscellaneous functions. Some of these functions may not be supported Some of these functions may not be supported depending on the implementation and the version of depending on the implementation and the version of SQL.SQL.

The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria described after the keyword where.

Conditional selections used in where clause: = Equal> Greater than< Less than>= Greater than or equal to

<= Less than or equal to<> Not equal toLIKE

Page 16: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

Select Select name, surname, facultyname, surname, faculty from from studentsstudents where where programprogram LIKE ‘ LIKE ‘ComCom%';%';

The names of the functions may also differ from The names of the functions may also differ from implementation to implementation. Here we are implementation to implementation. Here we are going to give a list of some important SQL going to give a list of some important SQL functions without too much details. functions without too much details.

COUNTCOUNT function returns the number of rows function returns the number of rows satisfying the condition in the WHERE clause. It satisfying the condition in the WHERE clause. It may be given with a wildcard argument like may be given with a wildcard argument like count (*). If COUNT is used without a WHERE count (*). If COUNT is used without a WHERE clause, it returns the number of records in the clause, it returns the number of records in the table.table.

SELECT Count(*) FROM SELECT Count(*) FROM studentsstudents;;

Page 17: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

SUMSUM returns the sum of all values in a column. It works returns the sum of all values in a column. It works only with numbers. Otherwise, an error message is only with numbers. Otherwise, an error message is broadcasted. broadcasted.

SELECT SELECT sumsum((examexam) FROM ) FROM studentsstudents WHERE WHERE FacultyFaculty = = ‘‘ElectronicsElectronics'; ';

AVGAVG function evaluates the average of a column. It function evaluates the average of a column. It works only with the numbers.works only with the numbers.

SELECT SELECT avgavg((examexam) FROM ) FROM students;students; MAXMAX function evaluates the largest value of a column. function evaluates the largest value of a column.

It can also work with character strings. In that cases the It can also work with character strings. In that cases the ascii values of the characters in the string is used to find ascii values of the characters in the string is used to find the maximum value.the maximum value.

SELECT SELECT maxmax((examexam) FROM ) FROM students;students;

Page 18: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

MINMIN function evaluates the smallest value of a function evaluates the smallest value of a column. It can also work with character column. It can also work with character strings. In that cases tha ascii values of the strings. In that cases tha ascii values of the characters in the string is used to find the characters in the string is used to find the minimum value.minimum value.

SELECT SELECT minmin((examexam) FROM ) FROM students;students; ADD_MONTHSADD_MONTHS function adds a number of function adds a number of

months to a specified data. The number of the months to a specified data. The number of the months to be added is specified in the second months to be added is specified in the second argument of the function. This function does argument of the function. This function does not work with the other data types without not work with the other data types without using any data convertor.using any data convertor.

Page 19: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

LAST_DAYLAST_DAY function returns the last day a spicified function returns the last day a spicified month. I works with data type data.month. I works with data type data.

MONTHS_BETWEENMONTHS_BETWEEN function returns the number function returns the number of months between two months. It needs two of months between two months. It needs two arguments: beginning and end data. It works with arguments: beginning and end data. It works with date tape data. It is sensitive to the order of the date tape data. It is sensitive to the order of the months.months.

NEXT_DAYNEXT_DAY function returns the name of the first function returns the name of the first day of the week which is equal to or later than day of the week which is equal to or later than another specified date.another specified date.

Page 20: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

SYSDATESYSDATE function returns the system time and date. function returns the system time and date. ABS ABS function returns the absolute value of the data.function returns the absolute value of the data. CEILCEIL function returns the smallest integer value function returns the smallest integer value

which is greater than or equal to the argument of the which is greater than or equal to the argument of the function.function.

FLOORFLOOR function returns the largest integer value function returns the largest integer value which is smaller than or equal to the argument of the which is smaller than or equal to the argument of the fuction.fuction.

Page 21: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

COSCOS function evaluates the cosine of the argument of function evaluates the cosine of the argument of the function. The argument is assumed to be given in the function. The argument is assumed to be given in radians.radians.

SINSIN function evaluates the sine of the argument of function evaluates the sine of the argument of the function. The argument is assumed to be given in the function. The argument is assumed to be given in radians.radians.

TANTAN function evaluates the tangent of the argument function evaluates the tangent of the argument of the function. The argument is assumed to be given of the function. The argument is assumed to be given in radians.in radians.

Page 22: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

COSHCOSH function evaluates the hyperbolic cosine of function evaluates the hyperbolic cosine of the argument of the function.the argument of the function.

SINHSINH function evaluates the hyperbolic sine of the function evaluates the hyperbolic sine of the argument of the function.argument of the function.

TANHTANH function evaluates the hyperbolic tangent of function evaluates the hyperbolic tangent of the argument of the function.the argument of the function.

EXPEXP function evaluates the power of the number function evaluates the power of the number e. e.

LN LN function evaluates the natural logarithm of the function evaluates the natural logarithm of the argument of the function.argument of the function.

Page 23: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

LOGLOG function needs two arguments. The first function needs two arguments. The first argument denotes the value whose logarithm will be argument denotes the value whose logarithm will be evaluated while the second argument specifies the evaluated while the second argument specifies the base of the logarithm.base of the logarithm.

MODMOD function evaluates the modulo of its first function evaluates the modulo of its first argument with respect to the divisor which is its argument with respect to the divisor which is its second argument. In contrast to module this function second argument. In contrast to module this function can deal with the real numbers.can deal with the real numbers.

Page 24: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

POWER POWER function evaluates the power of its first function evaluates the power of its first argument. The value of the power is given in the argument. The value of the power is given in the second argument.second argument.

SIGNSIGN function returns -1 if its argument is less than function returns -1 if its argument is less than 0, 0 if its argument is equal to 0, and 1 if its argument 0, 0 if its argument is equal to 0, and 1 if its argument is greater than 0.is greater than 0.

SQRTSQRT function evaluates the square root of its function evaluates the square root of its argument. The argument must be nonnegative.argument. The argument must be nonnegative.

SQL SQL supports some functions which perform certain supports some functions which perform certain actions on characters or character strings.actions on characters or character strings.

Page 25: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

CHRCHR function accepts numerical data as its argument function accepts numerical data as its argument and returns the character equivalent of this number.and returns the character equivalent of this number.

CONCAT CONCAT function adds its second argument to the function adds its second argument to the right hand side of its first argument. The result is a right hand side of its first argument. The result is a single string composed of these arguments.single string composed of these arguments.

INITCAPINITCAP function capitalizes the first letter of its function capitalizes the first letter of its argument which is assumed to be a string and makes argument which is assumed to be a string and makes all other characters lowercase.all other characters lowercase.

Page 26: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

LOWER LOWER changes all the characters in its argument to changes all the characters in its argument to lowercase.lowercase.

UPPERUPPER changes all the characters in its argument to changes all the characters in its argument to uppercase.uppercase.

SUBSTRSUBSTR has three arguments. The firs argument has three arguments. The firs argument which is to be operated on. The second argument which is to be operated on. The second argument defines the beginning of the substring which is defines the beginning of the substring which is extracted. And the last argument is the number of extracted. And the last argument is the number of characters in the substring to be extracted.characters in the substring to be extracted.

Page 27: BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,

LENGTHLENGTH returns the length of its argument. The returns the length of its argument. The length is the number of the characters in the string.length is the number of the characters in the string.

TO_CHARTO_CHAR converts its argument which is assumed converts its argument which is assumed to be a number into a character.to be a number into a character.

TO_NUMBERTO_NUMBER converts its argument which is converts its argument which is assumed to be a string into a number.assumed to be a string into a number.