Tuesday, 19 November 2024
SQL Queries
1) Write a query in SQL to find the minimum and maximum number from
the integer column
Select MAX(col1), MIN(col1) from table;
2) The following query shows the first row of the student table in the
output:
SELECT * FROM Student WHERE Rownum = 1;
3) The following query shows the last row of the student table in the
output:
SELECT * FROM Student WHERE Rowid = SELECT MAX(Rowid) from Studen
t;
4) The following query shows the last four rows of the Subject table:
SELECT * FROM (SELECT * FROM Subject order by Rowid DESC) WHERE Ro
wnum < = 4 ;
5) Syntax to find the Even rows from the table:
SELECT * FROM Table_Name WHERE MOD (Rowid,2) = 0 ;
6) The following query creates Student_Marks table from the existing
Student table:
CREATE TABLE Student_Marks SELECT * FROM Student;
7) What is a Foreign Key in PostgreSQL?
A foreign key is a column or a set of columns that establishes a link between
data in two tables.
1
Tuesday, 19 November 2024
8) To update data in a table, you can use the UPDATE statement. For
example:
UPDATE employees SET salary = 85000
WHERE name = 'John Doe’;
9) A view is a virtual table based on the result of a SELECT query. It allows
you to encapsulate complex queries and reuse them as if they were
tables. For example:
CREATE VIEW high_salary_employees AS
SELECT name, salary
FROM employees
WHERE salary > 80000;
10) To create an index in PostgreSQL, you can use the CREATE INDEX
statement. Indexes improve query performance by allowing faster
retrieval of records. For example:
CREATE INDEX idx_employee_name ON employees(name);