Question

In: Computer Science

create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key,...

create table node(
node_id integer primary key,
node_color varchar(10));

create table edge(
edge_id integer primary key,
origin_id integer,
destination_id integer,
foreign key (origin_id) references node(node_id),
foreign key (destination_id) references node(node_id));

write an SQL query that lists all those nodes that have edges with a destination node that has color 'red'.

Solutions

Expert Solution

Hi,

Please find below query as per your requirement.

Let me know if you have any doubt/concerns in this via comments.

Hope this answer helps you.

Thanks.

Solution:

Select n.node_id,n.node_color from node n 
inner join edge e on e.origin_id = n.node_id
inner join node n1 on n1.node_id = e.destination_id
where n1.node_color = 'red'; 

Here in this query we using inner join, which gives all matching values. Here node and edge matching basis on origin_id and node_id which satisfies condition of node having edges.

After that again using inner join on edge and node matching on the basis of node_id and destination_id, meaning we are checking the node who are destination node.

In where condition we are checking destination node color as 'red' i.e. filtering result on color.

Sample data used to test query :

insert into node values(1,'black');
insert into node values(2,'red');
insert into node values(3,'blue');
insert into node values(4,'red');

insert into edge values(1,1,2);
insert into edge values(2,2,3);
insert into edge values(3,3,4);

/***********Sample Output*************/


Related Solutions

Consider the following table definitions create table node( node_id integer primary key, node_color varchar(10)); create table...
Consider the following table definitions create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key, origin_id integer, destination_id integer, foreign key (origin_id) references node(node_id), foreign key (destination_id) references node(node_id)); What is the result of the following query? select node_id, node_color, destination_id from node, edge; An inner join of the tables node and edge that lists origin node_id and node_color together with the node_id of the destination node for all those nodes that have outgoing...
This is the database CREATE TABLE AIRCRAFT ( AC_NUMBER varchar(5) primary key, MOD_CODE varchar(10), AC_TTAF double,...
This is the database CREATE TABLE AIRCRAFT ( AC_NUMBER varchar(5) primary key, MOD_CODE varchar(10), AC_TTAF double, AC_TTEL double, AC_TTER double ); INSERT INTO AIRCRAFT VALUES('1484P','PA23-250',1833.1,1833.1,101.8); INSERT INTO AIRCRAFT VALUES('2289L','DC-90A',4243.8,768.9,1123.4); INSERT INTO AIRCRAFT VALUES('2778V','MA23-350',7992.9,1513.1,789.5); INSERT INTO AIRCRAFT VALUES('4278Y','PA31-950',2147.3,622.1,243.2); /* -- */ CREATE TABLE CHARTER ( CHAR_TRIP int primary key, CHAR_DATE date, AC_NUMBER varchar(5), CHAR_DESTINATION varchar(3), CHAR_DISTANCE double, CHAR_HOURS_FLOWN double, CHAR_HOURS_WAIT double, CHAR_TOT_CHG double, CHAR_OIL_QTS int, CUS_CODE int, foreign key (AC_NUMBER) references AIRCRAFT(AC_NUMBER) ); INSERT INTO CHARTER VALUES(10001,'2008-02-05','2289L','ATL',936,5.1,2.2,354.1,1,10011); INSERT INTO CHARTER VALUES(10002,'2008-02-05','2778V','BNA',320,1.6,0,72.6,0,10016);...
create table candidate ( cand_id   varchar(12) primary key,   -- cand_id name       varchar(40)           --...
create table candidate ( cand_id   varchar(12) primary key,   -- cand_id name       varchar(40)           -- cand_nm ); create table contributor ( contbr_id   integer primary key, name       varchar(40),           -- contbr_nm city     varchar(40),           -- contbr_city state       varchar(40),           -- contbr_st zip       varchar(20),           -- contbr_zip employer   varchar(60),           -- contbr_employer occupation   varchar(40)           -- contbr_occupation ); create table contribution ( contb_id   integer primary key, cand_id   varchar(12),           --...
CREATE TABLE Branch ( branchNo VARCHAR(4), address VARCHAR(50), city VARCHAR(30), state VARCHAR(2), phone VARCHAR(20), PRIMARY KEY...
CREATE TABLE Branch ( branchNo VARCHAR(4), address VARCHAR(50), city VARCHAR(30), state VARCHAR(2), phone VARCHAR(20), PRIMARY KEY (branchNo)); INSERT INTO Branch VALUES('B001','366 Tiger Ln','Los Angeles','CA','213-539-8600'); INSERT INTO Branch VALUES('B002','18 Harrison Rd','New Haven','CT','203-444-1818'); INSERT INTO Branch VALUES('B003','55 Waydell St','Essex','NJ','201-700-7007'); INSERT INTO Branch VALUES('B004','22 Canal St','New York','NY','212-055-9000'); INSERT INTO Branch VALUES('B005','1725 Roosevelt Ave','Queens','NY','718-963-8100'); INSERT INTO Branch VALUES('B006','1471 Jerrold Ave','Philadelphia','PA','267-222-5252'); CREATE TABLE Staff ( staffNo VARCHAR(4), fName VARCHAR(20), lName VARCHAR(20), position VARCHAR(20), sex VARCHAR(1), age INTEGER, salary NUMBER(8,2), phone VARCHAR(20), address VARCHAR(50), city VARCHAR(20),...
Consider the following SQL DDL statements: CREATE TABLE DEPT ( did INTEGER, dname VARCHAR(20), PRIMARY KEY(did));...
Consider the following SQL DDL statements: CREATE TABLE DEPT ( did INTEGER, dname VARCHAR(20), PRIMARY KEY(did)); CREATE TABLE EMP( eid INTEGER, name VARCHAR(20), did INTEGER, PRIMARY KEY(eid), FOREIGN KEY(did) REFERENCES DEPT); In the database created by above statements, which of the following operations may cause violation of referential integrity constraints? Question 1 options: UPDATE on DEPT INSERT into DEPT DELETE on EMP Both DELETE on EMP and INSERT into DEPT
CREATE TABLE Hotel ( roomNumber     INTEGER         PRIMARY KEY, type                  CHAR(1
CREATE TABLE Hotel ( roomNumber     INTEGER         PRIMARY KEY, type                  CHAR(10)         NOT NULL, rate                   INTEGER         NOT NULL, -- CONSTRAINT IC1 CHECK (type IN ('suite', 'king', 'queen')), CONSTRAINT IC2 CHECK (type <> 'suite' OR rate > 200), CONSTRAINT IC3 CHECK (NOT (type = 'king' AND (rate < 80 OR rate > 220))), CONSTRAINT IC4 CHECK (NOT (type = 'queen' AND rate >= 100)) ); which 8 of these inserts will be rejected only 8 are rejected 1. INSERT INTO Hotel VALUES (21, 'king', 90); 2. INSERT INTO Hotel...
CREATE TABLE youtubevideos( url VARCHAR(150), title VARCHAR(50), description VARCHAR(200), comid INTEGER NOT NULL, postuserVARCHAR(50) NOT NULL,...
CREATE TABLE youtubevideos( url VARCHAR(150), title VARCHAR(50), description VARCHAR(200), comid INTEGER NOT NULL, postuserVARCHAR(50) NOT NULL, postdate DATE, PRIMARY KEY (email), FOREIGN KEY (comid) REFERENCES Comedians(comid), FOREIGN KEY (postuser) REFERENCES Users(email)); CREATE TABLE Users( email VARCHAR(50), password VARCHAR(50), firstname VARCHAR(50), lastname VARCHAR(50), gender CHAR(1), age INTEGER, PRIMARY KEY (email)); CREATE TABLE Comedians( comid INTEGER, firstname VARCHAR(50), lastname VARCHAR(50), birthday DATE, VARCHAR(50), PRIMARY KEY(comid)); CREATE TABLE Reviews( reviewid INTEGER NOT NULL AUTO_INCREMENT, remark VARCHAR(100), rating CHAR(1), //P.F.G.E author VARCHAR(50) NOT NULL,...
Create table, create primary and foreign key constraints. Create index on the table to satisfy a...
Create table, create primary and foreign key constraints. Create index on the table to satisfy a query with aggregate functions.
CREATE TABLE campaign ( cmte_id             varchar(12), cand_id             varchar(12), cand_nm     &nbsp
CREATE TABLE campaign ( cmte_id             varchar(12), cand_id             varchar(12), cand_nm             varchar(40), contbr_nm           varchar(40), contbr_city         varchar(40), contbr_st           varchar(40), contbr_zip          varchar(20), contbr_employer     varchar(60), contbr_occupation   varchar(40), contb_receipt_amt   numeric(6,2), contb_receipt_dt    varchar(20), receipt_desc        varchar(40), memo_cd             varchar(20), memo_text           varchar(20), form_tp               varchar(20), file_num            varchar(20), tran_id             varchar(20), election_tp         varchar(20) Write SQL queries using the campaign data table. -- 4. show the candidate name and number of contributions, for each candidate -- Order by decreasing number of contributions. -- 5. show the candidate name and average contribution amount for each...
Create a table book_store with columns Book_id VARCHAR(255) NOT NULL, Book_Name VARCHAR(255) NOT NULL, Book_genre VARCHAR(255)...
Create a table book_store with columns Book_id VARCHAR(255) NOT NULL, Book_Name VARCHAR(255) NOT NULL, Book_genre VARCHAR(255) NOT NULL, Status VARCHAR(255) NOT NULL, PRIMARY KEY (Book_id) Create a table book with columns Book_id VARCHAR(255) NOT NULL, Book_Name VARCHAR(255) NOT NULL, Book_release integer, Book_price integer , Publisher Varchar(10), Book_genre VARCHAR(255) NOT NULL, PRIMARY KEY (Book_id) CREATE TABLE price_logs with columns id INT(11) NOT NULL AUTO_INCREMENT, Book_id VARCHAR(255) NOT NULL, Old_Book_price DOUBLE NOT NULL, New_Book_price DOUBLE NOT NULL, updated_at TIMESTAMP NOT NULL DEFAULT...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT