University of Cincinnati logo and link  
Basic SQL Statements
 
  UC ingot To look up data, we write a query beginning with SELECT.
  • The smallest SELECT statement must have a FROM clause as well, designating the table name.
    • SELECT * indicates that you want to select all columns.
    • So, we could have the simple statement:

    • SELECT * FROM STUDENTS
    • But, some tables have multiple columns, so if you want to state only specific columns to be returned, enter the column names, comma separated, in place of the *:

    • SELECT name, bearcatID FROM STUDENTS
  • If you only want to select certain records, add a WHERE clause.
    • SELECT name, bearcatID FROM STUDENTS WHERE HomeCollegeID = 32
  • Now, suppose you want to select records by joining students and colleges.  You'll need to specify both tables in the WHERE clause and tell the database how to join them together in the FROM clause.
    • SELECT name, bearcatID FROM STUDENTS, COLLEGES WHERE STUDENTS.HomeCollegeID = COLLEGES.CollegeID
  • But this returns all records for all students and colleges in our database, assuming that each student has a college ID represented in the colleges table.  So what if we want to only select students from college 32?  We use an AND statement.
    • SELECT name, bearcatID FROM STUDENTS, COLLEGES WHERE STUDENTS.HomeCollegeID = COLLEGES.CollegeID AND COLLEGES.CollegeID = 32
    • We could have easily stated STUDENTS.HomeCollegeID = 32.  Either way works.
    • 32 is not surrounded by quotes because it is a number.  If it were a string, we'd surround it with quotes.
    • SQL is case-insensitive, but the keywords (SELECT, FROM, WHERE) are traditionally entered in upper case.
 Action Queries