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

Slides:



Advertisements
Similar presentations
Return values.
Advertisements

Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
1 Sample SQL Applications Asst. Prof. Emin Korkut.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Using Single-Row Functions to Customize Output
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Spreadsheets Objective 6.02
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
Concepts of Database Management, Fifth Edition
Introduction to Databases Chapter 6: Understanding the SQL Language.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Queries. What is a query? A query is actually the name for any database manipulation operation. The most commonly used type is a select query, which is.
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
Lecture3b - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g ITBIS373 Database Development.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Open Source Server Side Scripting MySQL Functions
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Lecture 5 SQL FUNCTIONS.
Shelly Cashman: Microsoft Access 2016
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University, Informatics Institute, Maslak { 80626, Istanbul, Türkiye) Version

Sample SQL Applications This chapter is mainly devoted to some applications of SQL. There are various sample applications in this notes to explain and illustrate how SQL works. You can use interface at the URL or This chapter is mainly devoted to some applications of SQL. There are various sample applications in this notes to explain and illustrate how SQL works. You can use interface at the URL or It is possible to run many types of SQL applications and see the output through this interface. It is possible to run many types of SQL applications and see the output through this interface.

Queries Queries As you know SQL is an acronym for the statement Structured Query Language. However, SQL does a lot of things which are not included in the meaning of the term Query. As you know SQL is an acronym for the statement Structured Query Language. However, SQL does a lot of things which are not included in the meaning of the term Query. It creates, deletes, modifies, joins data and more. The data in a database is case sensitive while the SQL commands are case insensitive. This means that you can write SQL commands either in lowercase or uppercase. The data in a database is case sensitive while the SQL commands are case insensitive. This means that you can write SQL commands either in lowercase or uppercase.

The spacing is not important. That is, you can put spaces between the words as much as you want and you can give a command in a single or more than one lines at the SQL prompt. The spacing is not important. That is, you can put spaces between the words as much as you want and you can give a command in a single or more than one lines at the SQL prompt. SELECT command is one of the keywords which take important roles in SQL syntax and hence it is preferred to capitalize this command although nothing is wrong if it is given in lowercase letters. SELECT command is one of the keywords which take important roles in SQL syntax and hence it is preferred to capitalize this command although nothing is wrong if it is given in lowercase letters.

SELECT can not be used alone. It needs some parameters or other keywords. SELECT can not be used alone. It needs some parameters or other keywords. Therefore, if you just enter SELECT; at the sql prompt it will fail to work and the SQL program will complain by announcing a missing expression. Under the simplest conditions the column name of the table under consideration must be specified after SELECT. However, this is not sufficient because the table name has not been specified. select "column1"[,"column2",etc] from "tablename” [where "condition"]; [] = optional

This specification can be done by using the keyword FROM which must be followed by the name of the table under consideration. This specification can be done by using the keyword FROM which must be followed by the name of the table under consideration. Three important items under SQL are keyword, clause, and statement. The keyword refer to an individual SQL element like SELECT and FROM. A clause is a part of an SQL statement. For example, SELECT column1, column2, is a clause. A statement is a combination of SQL clauses. For example, you can combine a SELECT and FROM clause to write an SQL statement. Three important items under SQL are keyword, clause, and statement. The keyword refer to an individual SQL element like SELECT and FROM. A clause is a part of an SQL statement. For example, SELECT column1, column2, is a clause. A statement is a combination of SQL clauses. For example, you can combine a SELECT 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.

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; This will produce the following output through the interface we use here. select * from students; This will produce the following output through the interface we use here. Register_NoNameSurname Faculty ProgramExam AhmetYolcu Arts and Sciences Mathematics NazlıKoşan Electronics Comp. Eng UmutKarabasan Electronics Comp. Eng FatihKoklayan Construction Const. Eng SüzenKaşıkara Architecture BulutYağan Chem.--Met. Chem. Eng YağmurIslatan Mngmnt. Eng Ind. Eng.65 Students

select register_no, faculty, program, name, surname from students; results in the following display. Register_No FacultyProgramNameSurname Arts and Sciences MathematicsAhmetYolcu ElectronicsComp. Eng.NazlıKoşan ElectronicsComp. Eng.UmutKarabasan ConstructionConst. Eng.FatihKoklayan Architecture SüzenKaşıkara Chem.--Met.Chem. Eng.BulutYağan Mngmnt. Eng Ind. Eng.YağmurIslatan

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

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 = 1 8 / 2 = 0 8 / 3 = 2 9 / 3 = 0 The modulo operator does work only with integer data type.

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 expression 3 * /4 equals = 24 However, the expression 3 * (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.

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. ItemPrice Tomato Grape Tangerine Pomegranate Quince Chestnut Apple500000

select item, price, price*1.2 from prices; then we obtain the following display. ItemPricePrice*1.2 Tomato Grape Tangerine Pomegranate Quince Chestnut Apple

SQL Functions SQL Functions SQL can use some functions to realize certain predefined actions. A function is composed of three things: SQL can use some functions to realize certain predefined actions. A function is composed of three things: Name, Action, and Argument. Argument is mostly a columnname. Therefore, we can symbolically denote a function as functionname (columnname).

SQL functions can be categorized into classes which can be called aggregated functions, data and time functions, arithmetic functions, character functions, conversion functions, and miscellaneous functions. Some of these functions may not be supported depending on the implementation and the version of SQL. SQL functions can be categorized into classes which can be called aggregated functions, data and time functions, arithmetic functions, character functions, conversion functions, and miscellaneous functions. Some of these functions may not be supported depending on the implementation and the version of 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 to LIKE

Select name, surname, faculty from students where program LIKE ‘Com%'; Select name, surname, faculty from students where program LIKE ‘Com%'; The names of the functions may also differ from implementation to implementation. Here we are going to give a list of some important SQL functions without too much details. The names of the functions may also differ from implementation to implementation. Here we are going to give a list of some important SQL functions without too much details. COUNT function returns the number of rows satisfying the condition in the WHERE clause. It may be given with a wildcard argument like count (*). If COUNT is used without a WHERE clause, it returns the number of records in the table. COUNT function returns the number of rows satisfying the condition in the WHERE clause. It may be given with a wildcard argument like count (*). If COUNT is used without a WHERE clause, it returns the number of records in the table. SELECT Count(*) FROM students; SELECT Count(*) FROM students;

SUM returns the sum of all values in a column. It works only with numbers. Otherwise, an error message is broadcasted. SUM returns the sum of all values in a column. It works only with numbers. Otherwise, an error message is broadcasted. SELECT sum(exam) FROM students WHERE Faculty = ‘Electronics'; SELECT sum(exam) FROM students WHERE Faculty = ‘Electronics'; AVG function evaluates the average of a column. It works only with the numbers. AVG function evaluates the average of a column. It works only with the numbers. SELECT avg(exam) FROM students; SELECT avg(exam) FROM students; MAX function evaluates the largest value of a column. It can also work with character strings. In that cases the ascii values of the characters in the string is used to find the maximum value. MAX function evaluates the largest value of a column. It can also work with character strings. In that cases the ascii values of the characters in the string is used to find the maximum value. SELECT max(exam) FROM students; SELECT max(exam) FROM students;

MIN function evaluates the smallest value of a column. It can also work with character strings. In that cases tha ascii values of the characters in the string is used to find the minimum value. MIN function evaluates the smallest value of a column. It can also work with character strings. In that cases tha ascii values of the characters in the string is used to find the minimum value. SELECT min(exam) FROM students; SELECT min(exam) FROM students; ADD_MONTHS function adds a number of months to a specified data. The number of the months to be added is specified in the second argument of the function. This function does not work with the other data types without using any data convertor. ADD_MONTHS function adds a number of months to a specified data. The number of the months to be added is specified in the second argument of the function. This function does not work with the other data types without using any data convertor.

LAST_DAY function returns the last day a spicified month. I works with data type data. LAST_DAY function returns the last day a spicified month. I works with data type data. MONTHS_BETWEEN function returns the number of months between two months. It needs two arguments: beginning and end data. It works with date tape data. It is sensitive to the order of the months. MONTHS_BETWEEN function returns the number of months between two months. It needs two arguments: beginning and end data. It works with date tape data. It is sensitive to the order of the months. NEXT_DAY function returns the name of the first day of the week which is equal to or later than another specified date. NEXT_DAY function returns the name of the first day of the week which is equal to or later than another specified date.

SYSDATE function returns the system time and date. SYSDATE function returns the system time and date. ABS function returns the absolute value of the data. ABS function returns the absolute value of the data. CEIL function returns the smallest integer value which is greater than or equal to the argument of the function. CEIL function returns the smallest integer value which is greater than or equal to the argument of the function. FLOOR function returns the largest integer value which is smaller than or equal to the argument of the fuction. FLOOR function returns the largest integer value which is smaller than or equal to the argument of the fuction.

COS function evaluates the cosine of the argument of the function. The argument is assumed to be given in radians. COS function evaluates the cosine of the argument of the function. The argument is assumed to be given in radians. SIN function evaluates the sine of the argument of the function. The argument is assumed to be given in radians. SIN function evaluates the sine of the argument of the function. The argument is assumed to be given in radians. TAN function evaluates the tangent of the argument of the function. The argument is assumed to be given in radians. TAN function evaluates the tangent of the argument of the function. The argument is assumed to be given in radians.

COSH function evaluates the hyperbolic cosine of the argument of the function. COSH function evaluates the hyperbolic cosine of the argument of the function. SINH function evaluates the hyperbolic sine of the argument of the function. SINH function evaluates the hyperbolic sine of the argument of the function. TANH function evaluates the hyperbolic tangent of the argument of the function. TANH function evaluates the hyperbolic tangent of the argument of the function. EXP function evaluates the power of the number e. EXP function evaluates the power of the number e. LN function evaluates the natural logarithm of the argument of the function. LN function evaluates the natural logarithm of the argument of the function.

LOG function needs two arguments. The first argument denotes the value whose logarithm will be evaluated while the second argument specifies the base of the logarithm. LOG function needs two arguments. The first argument denotes the value whose logarithm will be evaluated while the second argument specifies the base of the logarithm. MOD function evaluates the modulo of its first argument with respect to the divisor which is its second argument. In contrast to module this function can deal with the real numbers. MOD function evaluates the modulo of its first argument with respect to the divisor which is its second argument. In contrast to module this function can deal with the real numbers.

POWER function evaluates the power of its first argument. The value of the power is given in the second argument. POWER function evaluates the power of its first argument. The value of the power is given in the second argument. SIGN function returns -1 if its argument is less than 0, 0 if its argument is equal to 0, and 1 if its argument is greater than 0. SIGN function returns -1 if its argument is less than 0, 0 if its argument is equal to 0, and 1 if its argument is greater than 0. SQRT function evaluates the square root of its argument. The argument must be nonnegative. SQRT function evaluates the square root of its argument. The argument must be nonnegative. SQL supports some functions which perform certain actions on characters or character strings. SQL supports some functions which perform certain actions on characters or character strings.

CHR function accepts numerical data as its argument and returns the character equivalent of this number. CHR function accepts numerical data as its argument and returns the character equivalent of this number. CONCAT function adds its second argument to the right hand side of its first argument. The result is a single string composed of these arguments. CONCAT function adds its second argument to the right hand side of its first argument. The result is a single string composed of these arguments. INITCAP function capitalizes the first letter of its argument which is assumed to be a string and makes all other characters lowercase. INITCAP function capitalizes the first letter of its argument which is assumed to be a string and makes all other characters lowercase.

LOWER changes all the characters in its argument to lowercase. LOWER changes all the characters in its argument to lowercase. UPPER changes all the characters in its argument to uppercase. UPPER changes all the characters in its argument to uppercase. SUBSTR has three arguments. The firs argument which is to be operated on. The second argument defines the beginning of the substring which is extracted. And the last argument is the number of characters in the substring to be extracted. SUBSTR has three arguments. The firs argument which is to be operated on. The second argument defines the beginning of the substring which is extracted. And the last argument is the number of characters in the substring to be extracted.

LENGTH returns the length of its argument. The length is the number of the characters in the string. LENGTH returns the length of its argument. The length is the number of the characters in the string. TO_CHAR converts its argument which is assumed to be a number into a character. TO_CHAR converts its argument which is assumed to be a number into a character. TO_NUMBER converts its argument which is assumed to be a string into a number. TO_NUMBER converts its argument which is assumed to be a string into a number.