This document provides an overview of basic SQL concepts including attribute ambiguity, aliasing, tuple variables, missing WHERE clauses, selecting all attributes with *, tables as sets, and set operations. It discusses how SQL allows duplicate tuples, qualifying attribute names, and retrieving distinct vs all values. Examples are provided for common SQL queries. The goal is to help students understand how to create and manipulate relational databases using SQL.
The document is a lecture on the SQL language given at Trinity College. It introduces SQL as the standard language for relational databases and describes it as declarative, meaning the user specifies the desired results without specifying how to retrieve them. It then discusses some basic SQL statements like CREATE TABLE to define the structure of tables and import data types for attributes.
This document discusses SQL injection vulnerabilities and techniques for exploiting them. It provides examples of SQL queries that can be used to enumerate data from a vulnerable database, including finding the number of columns, database version, system user, table names, and database names. It also demonstrates how to use SQL injection to create a backdoor PHP file ("c.php") that allows executing system commands via the cmd parameter, and provides an example of using this backdoor to run the shutdown command.
MySQL is an open-source relational database management system. The document discusses the introduction to MySQL, its development history, installation, features, data types, basic commands like CREATE, SELECT, UPDATE, DELETE. It also covers MySQL constraints. MySQL is widely used for web applications due to its speed, ease of use and open source nature. It can store and manage large volumes of data across different tables using relationships.
Paweł Cygal, a senior system administrator at Grand Parade, gives a presentation covering SQL injection and cross-site scripting (XSS) basics with examples using the Damn Vulnerable Web Application. The presentation defines SQL injection as a code injection technique used to attack data-driven applications by inserting malicious SQL statements. XSS enables attackers to inject client-side scripts by exploiting vulnerabilities in how a web application processes user input. Examples are provided of SQL injection and XSS vulnerabilities, along with solutions like prepared statements, input validation, and output encoding.
This document provides an introduction to SQL injection basics. It defines SQL injection as executing a SQL query or statement by injecting it into a user input field. The document outlines why SQL injection is studied, provides a sample database structure, and describes generic SQL queries and operators like UNION and ORDER BY. It also categorizes different types of SQL injection and attacks. The remainder of the document previews upcoming topics on blind SQL injection, data extraction techniques, and prevention.
This document discusses SQL injection vulnerabilities and techniques for exploiting them. It explains how user-supplied parameters can be manipulated to alter SQL queries and access unauthorized data or execute stored procedures. Some methods covered include adding SQL syntax like OR 1=1 to return all rows, using semicolons to concatenate queries, and prematurely terminating queries with characters like single quotes. The document also provides examples of forcing SQL errors to identify vulnerabilities and tips for retrieving excessive data or invoking stored procedures during an SQL injection attack.
The document is a lecture on the SQL language given at Trinity College. It introduces SQL as the standard language for relational databases and describes it as declarative, meaning the user specifies the desired results without specifying how to retrieve them. It then discusses some basic SQL statements like CREATE TABLE to define the structure of tables and import data types for attributes.
This document discusses SQL injection vulnerabilities and techniques for exploiting them. It provides examples of SQL queries that can be used to enumerate data from a vulnerable database, including finding the number of columns, database version, system user, table names, and database names. It also demonstrates how to use SQL injection to create a backdoor PHP file ("c.php") that allows executing system commands via the cmd parameter, and provides an example of using this backdoor to run the shutdown command.
MySQL is an open-source relational database management system. The document discusses the introduction to MySQL, its development history, installation, features, data types, basic commands like CREATE, SELECT, UPDATE, DELETE. It also covers MySQL constraints. MySQL is widely used for web applications due to its speed, ease of use and open source nature. It can store and manage large volumes of data across different tables using relationships.
Paweł Cygal, a senior system administrator at Grand Parade, gives a presentation covering SQL injection and cross-site scripting (XSS) basics with examples using the Damn Vulnerable Web Application. The presentation defines SQL injection as a code injection technique used to attack data-driven applications by inserting malicious SQL statements. XSS enables attackers to inject client-side scripts by exploiting vulnerabilities in how a web application processes user input. Examples are provided of SQL injection and XSS vulnerabilities, along with solutions like prepared statements, input validation, and output encoding.
This document provides an introduction to SQL injection basics. It defines SQL injection as executing a SQL query or statement by injecting it into a user input field. The document outlines why SQL injection is studied, provides a sample database structure, and describes generic SQL queries and operators like UNION and ORDER BY. It also categorizes different types of SQL injection and attacks. The remainder of the document previews upcoming topics on blind SQL injection, data extraction techniques, and prevention.
This document discusses SQL injection vulnerabilities and techniques for exploiting them. It explains how user-supplied parameters can be manipulated to alter SQL queries and access unauthorized data or execute stored procedures. Some methods covered include adding SQL syntax like OR 1=1 to return all rows, using semicolons to concatenate queries, and prematurely terminating queries with characters like single quotes. The document also provides examples of forcing SQL errors to identify vulnerabilities and tips for retrieving excessive data or invoking stored procedures during an SQL injection attack.
Ethical Hacking n VAPT presentation by Suvrat jainSuvrat Jain
a perfect example of your 6 weeks summer training ppt. Course-Ethical Hacking , its info and VAPT- Vulnerability Assessment n Penetration testing. about how vulnerability scanning , tools used , cracking password , etc.
SQL Injection
Project for lecture "Computer Systems Security"
You can find a SQL Injection Attack with sqlmap here: http://www.youtube.com/watch?v=wAwUv5dzwLk
It was performed for educational purposes ONLY.
The document discusses SQL injection attacks and how they work. SQL injection occurs when user input is inserted directly into an SQL query string without proper validation or escaping. This allows attackers to alter the structure of the intended SQL query and potentially gain unauthorized access to sensitive data or make unauthorized changes to the database. The document provides examples of vulnerable queries and how attackers can exploit them to inject malicious SQL code. It also lists some common techniques used in SQL injection attacks and provides recommendations for preventing SQL injection vulnerabilities.
Over 1.5 million customer records were stolen from T-Mobile Czech Republic by an employee. The records included names, email addresses, account numbers, but not location or traffic data. T-Mobile claims the perpetrator was caught trying to sell the database.
A hacking group in Russia allegedly used malware called Lurk to steal over 1.7 billion roubles (US $25.4 million) from bank accounts in Russia. Authorities arrested 50 people in connection with the scheme.
Github warned that a number of user accounts had been compromised through a password reuse attack related to recent data breaches at LinkedIn, MySpace, Tumblr and other sites that exposed over 642 million passwords.
The document discusses different types of SQL injection attacks, including tautologies, illegal/logically incorrect queries, union queries, piggybacked queries, and stored procedures. Tautologies aim to bypass authentication by making conditional statements always true. Illegal queries gather database information by causing syntax or type errors. Union queries extract data by combining results from multiple tables. Piggybacked queries maliciously execute additional queries by abusing query delimiters. Stored procedures can be used to escalate privileges or execute remote commands if vulnerabilities exist. Examples are provided for each type of attack along with potential solutions.
This document discusses SQL injection, which is a security vulnerability that allows attackers to interfere with how a database operates. SQL injection occurs when user input is not sanitized and is used directly in SQL queries, allowing attackers to alter the structure and meaning of queries. The document provides an example of how an attacker could log in without a password by adding SQL code to the username field. It also lists some common SQL injection techniques like using comments, concatenation, and wildcards. Finally, it points to additional online resources for learning more about SQL injection and database security.
This document provides an overview of SQL injection and a methodology for testing for SQL injection vulnerabilities. It begins with explanations of SQL and how SQL injection works. It then outlines a 7-step methodology for SQL injection testing, including input validation, information gathering, exploiting true/false conditions, extracting data, interacting with the operating system, using command prompts, and expanding influence. Methods described include exploiting different SQL statement types and database types.
SQL injection is a code injection technique that exploits vulnerabilities in database-driven web applications. It occurs when user input is not validated or sanitized for string literal escape characters that are part of SQL statements. This allows attackers to interfere with the queries and obtain unauthorized access to sensitive data or make changes to the database. The document then provides step-by-step instructions on how to scan for vulnerabilities, determine database details like name and tables, extract data like user credentials, bypass protections like magic quotes, and use tools to automate the process.
The presentation has a quick preamble on SQL injection definition, sqlmap and its key features.
I will then illustrate into details common and uncommon problems and respective solutions with examples that a penetration tester faces when he wants to take advantage of any kind of web application SQL injection flaw on real world web applications, for instance SQL injection in ORDER BY and LIMIT clauses, single entry UNION query SQL injection, specific web application technologies IDS bypasses and more.
These slides have been presented at the 2nd Digital Security Forum in Lisbon on June 27, 2009.
Updated version of http://www.slideshare.net/inquis/sql-injection-not-only-and-11.
The document discusses SQL injection attacks, including what SQL injection is, types of SQL injection attacks such as first and second order attacks, mechanisms for injection through user input or cookies, and techniques for preventing SQL injection like defensive coding practices and input validation. SQL injection is a code injection technique where malicious SQL statements are inserted into an entry field for execution by the backend database, allowing attackers to view or manipulate restricted data in the database. The document provides examples of SQL injection and explores ways attackers can infer information and encode attacks despite prevention methods.
The document provides an overview of the School of Computer Science programs at a university, including graduate programs (PhD, MS/PhD, MS), undergraduate programs (BS and BA in Computer Science, minor in CS), and a new Informatics program. It describes the research focus and opportunities for undergraduate and graduate students, including research labs, independent studies, honors programs, and paid REU and internship opportunities. Course requirements and tracks are outlined for the BS and BA in Computer Science. Transfer advising is also available to help students strategize their course plans.
The document summarizes a group advising meeting for computer science students at UMass. It discusses courses students are currently taking and will take in the future, including the 200-level core courses. It provides tips on meeting with advisors, maintaining tracking forms, considering study habits and involvement opportunities. It concludes by providing contact information for students to ask additional questions or have holds released on their accounts.
The document discusses the history and standardization of HTML. It notes that HTML was originally created by Tim Berners-Lee at CERN in 1990 for scientific purposes. The development of graphical web browsers by Mosaic and Netscape in the early 1990s and the subsequent "Browser Wars" led to HTML being defined by each browser instead of standards. The World Wide Web Consortium was founded in 1994 to establish standards for HTML. The document outlines that HTML standardization involves both syntax, which defines valid characters and strings, and semantics, which describes the meaning of elements.
This document contains notes from a Java review lecture. It discusses the Java API and various classes and interfaces in the java.util package for common data structures like lists, maps, and sets. It explains that these are interfaces, not classes, and describes some common implementations like LinkedList, ArrayList, HashMap, and HashSet. It also covers generics and how to define parameterized types to avoid casting. Finally, it discusses the for-each loop as a cleaner way to iterate over elements in collections compared to a traditional for loop.
The document discusses constraints in the relational model of databases. It covers domain constraints, which require that the value of each attribute in a tuple must be within the defined domain for that attribute. Key constraints are also discussed, which ensure that tuples within a relation are unique. Key constraints can be expressed in data definition language by specifying that no two tuples can have the same combination of values for a subset of attributes called a super key. A super key is a minimal set of attributes where no duplicate values are allowed.
Here are the steps to read a line of input from System.in:
1. Wrap System.in in an InputStreamReader
2. Wrap the InputStreamReader in a BufferedReader
3. Use the BufferedReader's readLine() method to read a full line of input as a String
So something like:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
This will read the input as a String rather than individual bytes.
Ethical Hacking n VAPT presentation by Suvrat jainSuvrat Jain
a perfect example of your 6 weeks summer training ppt. Course-Ethical Hacking , its info and VAPT- Vulnerability Assessment n Penetration testing. about how vulnerability scanning , tools used , cracking password , etc.
SQL Injection
Project for lecture "Computer Systems Security"
You can find a SQL Injection Attack with sqlmap here: http://www.youtube.com/watch?v=wAwUv5dzwLk
It was performed for educational purposes ONLY.
The document discusses SQL injection attacks and how they work. SQL injection occurs when user input is inserted directly into an SQL query string without proper validation or escaping. This allows attackers to alter the structure of the intended SQL query and potentially gain unauthorized access to sensitive data or make unauthorized changes to the database. The document provides examples of vulnerable queries and how attackers can exploit them to inject malicious SQL code. It also lists some common techniques used in SQL injection attacks and provides recommendations for preventing SQL injection vulnerabilities.
Over 1.5 million customer records were stolen from T-Mobile Czech Republic by an employee. The records included names, email addresses, account numbers, but not location or traffic data. T-Mobile claims the perpetrator was caught trying to sell the database.
A hacking group in Russia allegedly used malware called Lurk to steal over 1.7 billion roubles (US $25.4 million) from bank accounts in Russia. Authorities arrested 50 people in connection with the scheme.
Github warned that a number of user accounts had been compromised through a password reuse attack related to recent data breaches at LinkedIn, MySpace, Tumblr and other sites that exposed over 642 million passwords.
The document discusses different types of SQL injection attacks, including tautologies, illegal/logically incorrect queries, union queries, piggybacked queries, and stored procedures. Tautologies aim to bypass authentication by making conditional statements always true. Illegal queries gather database information by causing syntax or type errors. Union queries extract data by combining results from multiple tables. Piggybacked queries maliciously execute additional queries by abusing query delimiters. Stored procedures can be used to escalate privileges or execute remote commands if vulnerabilities exist. Examples are provided for each type of attack along with potential solutions.
This document discusses SQL injection, which is a security vulnerability that allows attackers to interfere with how a database operates. SQL injection occurs when user input is not sanitized and is used directly in SQL queries, allowing attackers to alter the structure and meaning of queries. The document provides an example of how an attacker could log in without a password by adding SQL code to the username field. It also lists some common SQL injection techniques like using comments, concatenation, and wildcards. Finally, it points to additional online resources for learning more about SQL injection and database security.
This document provides an overview of SQL injection and a methodology for testing for SQL injection vulnerabilities. It begins with explanations of SQL and how SQL injection works. It then outlines a 7-step methodology for SQL injection testing, including input validation, information gathering, exploiting true/false conditions, extracting data, interacting with the operating system, using command prompts, and expanding influence. Methods described include exploiting different SQL statement types and database types.
SQL injection is a code injection technique that exploits vulnerabilities in database-driven web applications. It occurs when user input is not validated or sanitized for string literal escape characters that are part of SQL statements. This allows attackers to interfere with the queries and obtain unauthorized access to sensitive data or make changes to the database. The document then provides step-by-step instructions on how to scan for vulnerabilities, determine database details like name and tables, extract data like user credentials, bypass protections like magic quotes, and use tools to automate the process.
The presentation has a quick preamble on SQL injection definition, sqlmap and its key features.
I will then illustrate into details common and uncommon problems and respective solutions with examples that a penetration tester faces when he wants to take advantage of any kind of web application SQL injection flaw on real world web applications, for instance SQL injection in ORDER BY and LIMIT clauses, single entry UNION query SQL injection, specific web application technologies IDS bypasses and more.
These slides have been presented at the 2nd Digital Security Forum in Lisbon on June 27, 2009.
Updated version of http://www.slideshare.net/inquis/sql-injection-not-only-and-11.
The document discusses SQL injection attacks, including what SQL injection is, types of SQL injection attacks such as first and second order attacks, mechanisms for injection through user input or cookies, and techniques for preventing SQL injection like defensive coding practices and input validation. SQL injection is a code injection technique where malicious SQL statements are inserted into an entry field for execution by the backend database, allowing attackers to view or manipulate restricted data in the database. The document provides examples of SQL injection and explores ways attackers can infer information and encode attacks despite prevention methods.
The document provides an overview of the School of Computer Science programs at a university, including graduate programs (PhD, MS/PhD, MS), undergraduate programs (BS and BA in Computer Science, minor in CS), and a new Informatics program. It describes the research focus and opportunities for undergraduate and graduate students, including research labs, independent studies, honors programs, and paid REU and internship opportunities. Course requirements and tracks are outlined for the BS and BA in Computer Science. Transfer advising is also available to help students strategize their course plans.
The document summarizes a group advising meeting for computer science students at UMass. It discusses courses students are currently taking and will take in the future, including the 200-level core courses. It provides tips on meeting with advisors, maintaining tracking forms, considering study habits and involvement opportunities. It concludes by providing contact information for students to ask additional questions or have holds released on their accounts.
The document discusses the history and standardization of HTML. It notes that HTML was originally created by Tim Berners-Lee at CERN in 1990 for scientific purposes. The development of graphical web browsers by Mosaic and Netscape in the early 1990s and the subsequent "Browser Wars" led to HTML being defined by each browser instead of standards. The World Wide Web Consortium was founded in 1994 to establish standards for HTML. The document outlines that HTML standardization involves both syntax, which defines valid characters and strings, and semantics, which describes the meaning of elements.
This document contains notes from a Java review lecture. It discusses the Java API and various classes and interfaces in the java.util package for common data structures like lists, maps, and sets. It explains that these are interfaces, not classes, and describes some common implementations like LinkedList, ArrayList, HashMap, and HashSet. It also covers generics and how to define parameterized types to avoid casting. Finally, it discusses the for-each loop as a cleaner way to iterate over elements in collections compared to a traditional for loop.
The document discusses constraints in the relational model of databases. It covers domain constraints, which require that the value of each attribute in a tuple must be within the defined domain for that attribute. Key constraints are also discussed, which ensure that tuples within a relation are unique. Key constraints can be expressed in data definition language by specifying that no two tuples can have the same combination of values for a subset of attributes called a super key. A super key is a minimal set of attributes where no duplicate values are allowed.
Here are the steps to read a line of input from System.in:
1. Wrap System.in in an InputStreamReader
2. Wrap the InputStreamReader in a BufferedReader
3. Use the BufferedReader's readLine() method to read a full line of input as a String
So something like:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
This will read the input as a String rather than individual bytes.
1. Trinity College
Basic SQL
Part 2
Timothy Richards
Trinity College, Hartford CT • Department of Computer Science • CPSC 372
2. Last Time
• SQL Language
• Declarative language
• Many Standards
• 1986-2008
• Data Definition Language (DDL)
• CREATE TABLE
• Data Manipulation Language (DML)
• SELECT-FROM-WHERE
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 2
3. Today
• Objectives
• Ambiguous attribute names, Aliasing, Renaming, and Tuple Variables
• Unspecified WHERE and the use of *
• Tables as sets
• Pattern matching and arithmetic operators
• Ordering query results
• INSERT, DELETE, UPDATE
• Goal
After today’s class you should be able to create a relational
database schema/instance using SQL DDL, manipulate data in the
tables you create, and define queries on that data.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 3
4. Query Example
For every project located in ‘Stafford’, list the
project number, the controlling department number,
and the department manager’s last name, address,
and birth date.
SELECT Pnumber, Dnum,
Lname, Address,
Bdate
FROM PROJECT, DEPARTMENT,
EMPLOYEE
WHERE Dnum=Dnumber AND
Mgr_ssn=Ssn AND
Plocation=‘Stafford’;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 4
5. Attribute Ambiguity
In SQL, the same name can be used for two (or more)
attributes as long as the attributes are in different
relations.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 5
6. Attribute Ambiguity
In SQL, the same name can be used for two (or more)
attributes as long as the attributes are in different
relations.
Ambiguous
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 6
7. Attribute Ambiguity
In SQL, the same name can be used for two (or more)
attributes as long as the attributes are in different
relations.
Ambiguous
Solution: qualify attributes names
with the relation names to avoid ambiguity
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 7
8. Attribute Ambiguity
Imagine the Dno and Lname attributes
of the EMPLOYEE relation were called
Dnumber and Name and the Dname attribute
of DEPARTMENT was called Name.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 8
9. Attribute Ambiguity
Imagine the Dno and Lname attributes
of the EMPLOYEE relation were called
Dnumber and Name and the Dname attribute
of DEPARTMENT was called Name.
List the first name, last name, and address
of all employees in the Research department.
SELECT Fname, Name, Address
FROM EMPLOYEE, DEPARTMENT
WHERE Name=‘Research’ AND
Dnumber=Dnumber;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
10. Attribute Ambiguity
Imagine the Dno and Lname attributes
of the EMPLOYEE relation were called
Dnumber and Name and the Dname attribute
of DEPARTMENT was called Name.
List the first name, last name, and address
of all employees in the Research department.
SELECT Fname, Name, Address
FROM EMPLOYEE, DEPARTMENT
WHERE Name=‘Research’ AND Ambiguity
Dnumber=Dnumber;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 10
11. Attribute Ambiguity
Imagine the Dno and Lname attributes
of the EMPLOYEE relation were called
Dnumber and Name and the Dname attribute
of DEPARTMENT was called Name.
List the first name, last name, and address
of all employees in the Research department.
Qualify!
SELECT Fname, EMPLOYEE.Name, Address
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.Name=‘Research’ AND
DEPARTMENT.Dnumber=EMPLOYEE.Dnumber;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
12. Attribute Ambiguity
You can also qualify attribute names for clarity...
SELECT EMPLOYEE.Fname, EMPLOYEE.Name,
EMPLOYEE.Address
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.Name=‘Research’ AND
DEPARTMENT.Dnumber=EMPLOYEE.Dnumber;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 12
13. Attribute Ambiguity
You can also qualify attribute names for clarity...
This is quite verbose!
SELECT EMPLOYEE.Fname, EMPLOYEE.Name,
EMPLOYEE.Address
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.Name=‘Research’ AND
DEPARTMENT.Dnumber=EMPLOYEE.Dnumber;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
14. Attribute Ambiguity
You can also qualify attribute names for clarity...
This is quite verbose!
SELECT E.Fname, E.Name, E.Address
FROM EMPLOYEE AS E, DEPARTMENT AS D
WHERE D.Name=‘Research’ AND
D.Dnumber=E.Dnumber;
For this we can use table aliases!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 14
15. Attribute Ambiguity
You can also qualify attribute names for clarity...
This is quite verbose!
SELECT E.Fname, E.Name, E.Address
FROM EMPLOYEE AS E, DEPARTMENT AS D
WHERE D.Name=‘Research’ AND
D.Dnumber=E.Dnumber;
Although this is nice for reducing typing...
it is required for queries that refer to the same relation twice!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 15
16. Attribute Ambiguity
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
Although this is nice for reducing typing...
it is required for queries that refer to the same relation twice!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 16
17. Attribute Ambiguity
What does this query mean in English?
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
Although this is nice for reducing typing...
it is required for queries that refer to the same relation twice!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 17
18. Attribute Ambiguity
What does this query mean in English?
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
For each employee, retrieve the employee’s first
and last name and the first and last name
of his or her immediate supervisor.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 18
19. Attribute Ambiguity
What does this query mean in English?
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
Tuple Variables
For each employee, retrieve the employee’s first
and last name and the first and last name
of his or her immediate supervisor.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 19
20. Attribute Ambiguity
It is also possible to rename all attributes
EMPLOYEE AS E(Fn,Mi,Ln,Ssn,Bd,Addr,Sx,Sal,Sssn,Dno)
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 20
21. Missing WHERE
What if I do not want to impose a condition
on the tuples that are returned?
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 21
22. Missing WHERE
What if I do not want to impose a condition
on the tuples that are returned?
Turns out that you do not need a WHERE clause!
SELECT Ssn
FROM EMPLOYEE;
SELECT Ssn, Dname
FROM EMPLOYEE, DEPARTMENT;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
23. Missing WHERE
What if I do not want to impose a condition
on the tuples that are returned?
Turns out that you do not need a WHERE clause!
SELECT Ssn
FROM EMPLOYEE;
What tuples does SELECT Ssn, Dname
this return? FROM EMPLOYEE, DEPARTMENT;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 23
24. Missing WHERE
Because we do not specify a “join condition” between
the two tables, we get the cross product of
the specified attributes between the two tables!
What tuples does SELECT Ssn, Dname
this return? FROM EMPLOYEE, DEPARTMENT;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 24
25. Missing WHERE
SSN
E 02783465
02537596
Dname
D Research
Administration
Because we do not specify a “join condition” between
the two tables, we get the cross product of
the specified attributes between the two tables!
What tuples does SELECT Ssn, Dname
this return? FROM EMPLOYEE, DEPARTMENT;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 25
26. Missing WHERE
SSN
E 02783465 SSN Dname
02537596 02783465 Research
* 02537596 Research
Dname 02783465 Administration
D Research 02537596 Administration
Administration
Because we do not specify a “join condition” between
the two tables, we get the cross product of
the specified attributes between the two tables!
What tuples does SELECT Ssn, Dname
this return? FROM EMPLOYEE, DEPARTMENT;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 26
27. Missing WHERE
SSN
E 02783465 SSN Dname
02537596 02783465 Research
* 02537596 Research
Dname 02783465 Administration
D Research 02537596 Administration
Administration
We can get sensible results
if we include a join condition on
the SSN attribute.
What tuples does SELECT Ssn, Dname
this return? FROM EMPLOYEE, DEPARTMENT;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
28. Missing WHERE
SSN
E 02783465
SSN Dname
02537596
* 02783465 Research
Dname 02537596 Administration
D Research
Administration
We can get sensible results
if we include a join condition on
the SSN attribute.
What tuples does SELECT Ssn, Dname
this return? FROM EMPLOYEE, DEPARTMENT
WHERE Dno=Dnumber;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
29. Selecting *
Sometimes we want all the attributes
returned in a result.
SELECT *
FROM EMPLOYEE;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 29
30. Selecting *
Sometimes we want all the attributes
returned in a result.
SELECT *
FROM EMPLOYEE;
This will return each value
for every attribute in a tuple.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 30
31. Tables as Sets
SQL typically treats a table as a set.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 31
32. Tables as Sets
SQL typically treats a table as a set.
More accurately, as a multiset.
What is a multiset?
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
33. Tables as Sets
SQL typically treats a table as a set.
More accurately, as a multiset.
What is a multiset?
A set, where duplicate tuples
can appear more than once in a table.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 33
34. Tables as Sets
SQL typically treats a table as a set.
More accurately, as a multiset.
What is a multiset?
A set, where duplicate tuples
can appear more than once in a table.
But, I thought that was bad?!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 34
35. Tables as Sets
SQL typically treats a table as a set.
More accurately, as a multiset.
What is a multiset?
A set, where duplicate tuples
can appear more than once in a table.
But, I thought that was bad?!
Turns out, in practice this is useful...
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 35
36. Tables as Sets
• SQL allows duplicates because...
• Duplicate elimination is an expensive operation.
Can you think why?
• The user may want to see duplicate tuples in the result of a
query
• When an aggregate function is applied to tuples, in most
cases we do not want to eliminate duplicates.
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36
37. Tables as Sets
Retrieve the salary of every employee
SELECT Salary
FROM EMPLOYEE;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 37
38. Tables as Sets
Retrieve the salary of every employee
SELECT Salary
FROM EMPLOYEE;
Ok, this returns tuples containing the salary
of each employee.
Is it useful?
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 38
39. Tables as Sets
Retrieve the salary of every employee
SELECT Salary
FROM EMPLOYEE;
Ok, this returns tuples containing the salary
of each employee.
Is it useful?
What if we only wanted to see the distinct salaries?
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 39
40. Tables as Sets
Retrieve the salary of every employee
SELECT DISTINCT Salary
FROM EMPLOYEE;
Ok, this returns tuples containing the salary
of each employee.
Is it useful?
What if we only wanted to see the distinct salaries?
This results in distinct salaries!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 40
41. Tables as Sets
Retrieve the salary of every employee
We can use
SELECT ALL Salary
ALL to retrieve all
---same as default. FROM EMPLOYEE;
Ok, this returns tuples containing the salary
of each employee.
Is it useful?
What if we only wanted to see the distinct salaries?
This results in distinct salaries!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 41
42. Tables as Sets
SQL has direct support for some set operations
Make a list of all project numbers for projects
that involve an employee whose last name is ‘Smith’,
either as a worker or as a manager of the department
that controls the project.
If SQL supports set union (it does), how might
we want to compose this query?
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 42
43. Tables as Sets
SQL has direct support for some set operations
Make a list of all project numbers for projects
that involve an employee whose last name is ‘Smith’,
either as a worker or as a manager of the department
that controls the project.
SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE First write the query
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn for the first part...
AND Lname=‘Smith’;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 43
44. Tables as Sets
SQL has direct support for some set operations
Make a list of all project numbers for projects
that involve an employee whose last name is ‘Smith’,
either as a worker or as a manager of the department
that controls the project.
SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn
AND Lname=‘Smith’;
Then write a query for
SELECT DISTINCT Pnumber the second part...
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Essn=Ssn AND Lname=‘Smith’;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 44
45. Tables as Sets
SQL has direct support for some set operations
Make a list of all project numbers for projects
that involve an employee whose last name is ‘Smith’,
either as a worker or as a manager of the department
that controls the project.
(SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn
AND Lname=‘Smith’) Then union the
UNION results together!
(SELECT DISTINCT Pnumber
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Essn=Ssn AND Lname=‘Smith’);
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 45
46. Tables as Sets
SQL has direct support for some set operations
In addition to UNION SQL also supports
set difference (EXCEPT) and
set intersection (INTERSECT)
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 46
47. Substring Pattern Matching
Retrieve all employees whose address is in Houston,Texas.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Address LIKE ‘%Houston,TX%’;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 47
48. Substring Pattern Matching
Find all employees who were born during the 1950s.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Bdate LIKE ‘__5_______’;
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 48
49. Arithmetic Operations
Show the resulting salaries if every employee working on
the ‘ProductX’ project is given a 10 percent raise.
SELECT E.Fname, E.Lname, E.Salary*1.1 AS incr
FROM EMPLOYEE AS E, WORKS_ON AS W, PROJECT AS P
WHERE E.Ssn=W.Essn AND W.Pno=P.Pnumber AND
P.Pname=‘ProductX’
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 49
50. Next time...
Ordering Query Results
AND
Insert, Deleting, and Updating, Oh My!
Trinity College, Hartford CT • Department of Computer Science • CPSC 372 50